Batch to find file, copy file to another directory

大城市里の小女人 提交于 2019-12-12 03:18:04

问题


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

  1. 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
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!