adm Posted October 13, 2011 Share Posted October 13, 2011 Hello,I need a batch script which will move multiple files into newly created folders with the same name. For example,abc101.pdfabc102.pdfabc103.tifeach file needs to be moved into a newly created folder with the same name minus the extension.abc101.pdf => folder abc001abc102.pdf => folder abc102abc103.tif => folder abc103Thanks in advance for your help. Link to comment Share on other sites More sharing options...
Scr1ptW1zard Posted October 13, 2011 Share Posted October 13, 2011 This will create the folders in the same directory as the files you are working against.@echo off::Change this variable to the directory containing the filesSet "fldr=D:\Your Files Location"for /f "tokens=*" %%a in ('dir "%fldr%" /a-d /b') do md "%fldr%\%%~na"&move "%fldr%\%%a" "%fldr%\%%~na" Link to comment Share on other sites More sharing options...
adm Posted October 13, 2011 Author Share Posted October 13, 2011 Thanks, Scr1ptW1zard! Worked like a charm. If I decide I only want to move the PDF files to new folders, can I modify the beginning of the last line to:for /f "token=*.pdf"I appreciate your help. Link to comment Share on other sites More sharing options...
Scr1ptW1zard Posted October 14, 2011 Share Posted October 14, 2011 Actually, to only list *.pdf files, you would need to modify the "dir" command line syntax that is listing the files.In the script, I have the following command line that is listing all files in a given directory:dir "%fldr%" /a-d /bWhat this is stating is "list all items in a bare, just display the file name, format (/b ) that are not directories (/a-d) in the specified directory (%fldr%)"To only show .pdf files, you would need to modify this command to the following:dir "%fldr%\*.pdf" /a-d /bTo get a better understanding of what these command lines are doing, open a command prompt and run them from there, changing the %fldr% to a valid path.Something like this:dir "%userprofile%\documents\*.pdf" /a-d /bFor a full list of command line switches for the 'dir' command, run dir /? at the command prompt.And if you want more information on what the "tokens=*" is, look at the help for the "for" command: ( for /? ) So, your new batch file would look like this:@echo off::Change this variable to the directory containing the filesSet "fldr=D:\Your Files Location"for /f "tokens=*" %%a in ('dir "%fldr%\*.pdf" /a-d /b') do md "%fldr%\%%~na"&move "%fldr%\%%a" "%fldr%\%%~na" Link to comment Share on other sites More sharing options...
adm Posted October 14, 2011 Author Share Posted October 14, 2011 Thanks so much for the info! I really appreciate it. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now