Reputation: 159
I want to create a batch file which will search for .docx files in my folder and then for each of them create a separate directory and then copy each .docx file inside that directory. My folder contains of hundreds of files with so long names and housekeeping is a nightmare!
I have searched around and found some code which helps me to create a new directory for each .docx file. Here is the link.. "Win 7: CMD batch file for creating directories based on filenames"
The code which ONLY creates new directory for the file
for %%I in (*.docx) do mkdir "%%~pnI"
However how do I COPY the file INTO the newly created directory with the same name?
E.g file name is 20160611150424.docx (where numbers are YYYY MM DD HH MM SS) and using the code above it creates a file called 20160611150424.
How do i use batch file to copy this file into this folder? Which is the appropriate code to be used?
Many thanks,
NewLearner
Upvotes: 1
Views: 1940
Reputation: 30238
Read copy /?
: you could copy a file into newly created directory specifying the destination as a folder without file name and extension and copy
command will retain them):
for %%I in (*.docx) do (
mkdir "%%~pnI" 2>NUL
copy /B "%%~fI" "%%~pnI\"
)
or specify the destination with file name and extension explicitly as follows:
for %%I in (*.docx) do (
mkdir "%%~pnI" 2>NUL
copy /B "%%~fI" "%%~pnI\%%~nxI"
)
Note that 2>NUL
will suppress error message A subdirectory or file a already exists
possibly generated by mkdir
command if this is the case.
Resources (required reading):
%%~nxI
etc. special page) Command Line arguments (Parameters)2>NUL
etc. special page) RedirectionUpvotes: 0