views:

544

answers:

4

Say you have 100 directories and for each directory you have a file named .pdf stored somewhere else. If you want to move/copy each file into the directory with the same name, can this be done on the Windows command line?

A: 

You would need to write a script to iterate through each file (and its path), extract the filename-'.pdf' and then move the file to the directory of the same name

Liam
+1  A: 

You can do it using the FOR command. Something in the line of:

for /f %%f in ('dir /s /b c:\source\*.pdf') do copy "%%f" c:\target

If you have a list of the file names w/ full path in a text file, say files.txt, you can also do

for /f %%f in (files.txt) do copy "%%f" c:\target
Franci Penov
+3  A: 

This is a batch script that probably does what you want:

setlocal
set target_dir=D:\
set source_dir=C:\WINDOWS

for %%i in (%source_dir%\*.pdf) do move %%i %target_dir%\%%~ni.%%~xi

endlocal
ΤΖΩΤΖΙΟΥ
+1  A: 

From the command line:

for /f %f in ('dir /s /b mypath\*.pdf') do @copy %~nxf myotherpath

As it's on a command line and not in a batch file you only need %, not %%.

dir /s /b is recursive and bare. (see dir /?)

The @ before copy stops the echo of each copy command. You can echo them if you like, up to you.

%~nxf gets the name and extension of %f. (see call /?)

Richard A