问题
My question is very similar to the question posted here on stackoverflow but the difference for me is that I need the batch script to do the following
- Look at a csv file which has the name only without the image extension and find the image with that name in a particular directory
- Then take that file and copy it to another directory
What modifications would I need to do to this batch script to accomplish that task?
@echo off
for /f "tokens=1,2 delims=," %%j in (project.csv) do (
copy "%%j.jpg" c:\mytestproject\newimages
rename c:\mytestproject\newimages\%%j.jpg" %%k.jpg
)
回答1:
The following Batch file assumes that the .CSV file contains just one field per line: the name, with NO extension, of a file that exist, with ANY extension, in a particular directory, so it copy that file to another directory.
@echo off
set "theDir=C:\The\Particular\Directory"
for /F "delims=" %%f in (theFile.csv) do (
copy %theDir%\%%f.* "C:\another\Directory"
)
If you want the image file have an extension taken from a limited list:
@echo off
set "theDir=C:\The\Particular\Directory"
for /F "delims=" %%f in (theFile.csv) do (
for %%e in (jpg png) do (
if exist "%theDir%\%%f.%%e" copy "%theDir%\%%f.%%e" "C:\another\Directory"
)
回答2:
You can check for existence of a file with if exist. So in your loop:
if exist "%%j.jpg" copy "%%j.jpg" target
if exist "%%j.png" copy "%%j.png" target
来源:https://stackoverflow.com/questions/8955947/batch-to-find-file-copy-file-to-another-directory