Jump to content

Recommended Posts

Posted

Hello,

I need a batch script which will move multiple files into newly created folders with the same name. For example,

abc101.pdf

abc102.pdf

abc103.tif

each file needs to be moved into a newly created folder with the same name minus the extension.

abc101.pdf => folder abc001

abc102.pdf => folder abc102

abc103.tif => folder abc103

Thanks in advance for your help.


Posted

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 files
Set "fldr=D:\Your Files Location"

for /f "tokens=*" %%a in ('dir "%fldr%" /a-d /b') do md "%fldr%\%%~na"&move "%fldr%\%%a" "%fldr%\%%~na"

Posted

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.

Posted

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 /b

What 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 /b

To 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 /b

For 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 files
Set "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"

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...