测试IF文件是否存在,ELSE xcopy这两个文件 [英] Test IF file exist, ELSE xcopy these two files

查看:88
本文介绍了测试IF文件是否存在,ELSE xcopy这两个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好.

所以我花了很多时间试图将我的代码拼凑在一起(对其他帖子的各种回复),以查看是否可以得到一些有用的东西.不行我对酱油已经迷失了,现在不得不向您寻求帮助.

背景: 作业系统:Windows 10 我每晚使用程序text2folders.exe在辅助驱动器上创建20-30个新文件夹. 首先,我有一个基本文件"aGallery-dl.bat",我使用xcopy批处理文件填充了每个文件夹.其次,我不时使用相同的xcopy更新源文件"aGallery-dl.bat",这会覆盖较旧的目标文件,并用最新的"aGallery-dl.bat"填充所有文件夹 (他们是否需要).一切都很好.

@echo off
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy /y /d ".\aGallery-dl.bat" "%%a\"

我最近决定要向每个文件夹添加两个新文件,并扩展了我的xcopy使其包含这些文件.一切都很好.

@echo off
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy /y /d ".\aGallery-dl.bat" "%%a\"
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy ".\Folder.jpg" "%%a\"
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy ".\Folder2.jpg" "%%a\"

Folder.jpg 大红色X

Folder2.jpg 大黄色!标记

当我选择在给定文件夹(同样是100的一个)中运行"aGallery-dl.bat"时,它首先删除Folder.jpg,然后将Folder2.jpg重命名为Folder.jpg.这具有将红色X替换为黄色的效果!在文件资源管理器"父文件夹中查看文件夹时.其次,它称为"gallery-dl.exe".我使用从红色到黄色的方式来告诉我至少运行过一次"aGallery-dl.bat".一切都很好.

rem @echo off
del .\Folder.jpg
ren .\Folder2.jpg Folder.jpg
FOR /F %%i IN ('cd') DO set FOLDER=%%~nxi
"C:\Program Files (x86)\gallery-dl\gallery-dl.exe" -d "U:\11Web\gallery-dl" --download-archive ".\aGDB.sqlite3" "https://www.deviantart.com/"%FOLDER%"/gallery/all"
del .\Folder.jpg

如果"aGallery-dl.bat"成功完成,它将最终删除Folder.jpg(当前为黄色!),现在可以看到该文件夹​​的代表性内容(通常是DeviantArt .jpg).

问题: 当我必须重新运行我的原始xcopy命令以更新"ALL FOLDERS"中的"aGallery-dl.bat"时,Folder.jpg和Folder2.jpg将被重新复制到所有文件夹,从而无法通过"aGallery-dl.bat."我不想回过头来断断续续地在100个文件夹中重新运行"aGallery-dl.bat"(再次,只有那些拥有aGallery-dl.bat的文件夹至少运行一次).我需要某种类型的测试,如果目标文件夹中已经存在"aGallery-dl.bat",请不要在下面使用xcopy Folder.jpg和Folder2.jpg这样的模糊示例.

*********************************Some sort of test statement here!!!***********************

:aGallery-dlPresent
GOTO eof

:aGallery-dlNotPresent
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy ".\Folder.jpg" "%%a\"
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy ".\Folder2.jpg" "%%a\"
GOTO eof

:eof

我在下面找到了一个有希望的候选测试语句(从我在其他文章中所读的内容/原版复制了它的原始形式),但是我在寻找想法/替代方法,因为我没有想法如何修改/注入/实现以下内容以使其在上面起作用.

If exist \\%DIR%\%Folder%\123456789.wav xcopy \\%DIR%\%Folder%\123456789.wav D:\%New Folder%\ /y

使用XCopy复制文件且不覆盖前一个文件(如果没有提示的话)

注意:以下内容是所有内容的大致表述(除非有正确的-test语句-).

rem @echo off
*********************************Some sort of test statement here!!!***********************

:aGallery-dlPresent
GOTO eof

:aGallery-dlNotPresent
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy ".\Folder.jpg" "%%a\"
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy ".\Folder2.jpg" "%%a\"
GOTO eof

:eof

for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy /y /d ".\aGallery-dl.bat" "%%a\"

解决方案

用于复制文件的命令为 COPY .它是Windows命令处理器cmd.exe的内部命令. XCOPY 是在目录%SystemRoot%\System32中扩展的文件和目录复制可执行文件,由于Windows Vista具有更强大的 ROBOCOPY 功能,自Windows Vista以来已弃用该文件和目录具有完整的合格文件名%SystemRoot%\System32\robocopy.exe.

此简单的文件复制任务无需使用 XCOPY ROBOCOPY . COPY 对于源文件aGallery-dl.batFolder.jpgFolder2.jpg没有隐藏属性集,并且目标目录中的相同文件没有只读属性集就足够了./p>

.\引用当前目录,该目录可以是任何目录.双击批处理文件时,Windows资源管理器会将批处理文件的目录设置为当前目录.但这几乎是运行批处理文件的唯一方法,在批处理文件上,已执行的批处理文件的目录会自动设置为当前目录(批处理文件存储在使用UNC路径访问的网络资源上).

%~dp0用来引用批处理文件的路径.该路径始终以反斜杠结尾,这意味着在将批处理文件路径与文件或文件夹名称连接在一起时,不需要其他反斜杠. %~dp0的使用使得可以在与执行的批处理文件相同的目录中引用文件,而与执行批处理文件的当前目录是哪个目录无关.

您的任务所需的批处理文件为:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /D %%I in ("U:\11Web\gallery-dl\deviantart\*") do (
    if not exist "%%I\aGallery-dl.bat" (
        copy "%~dp0Folder.jpg" "%%I\"
        copy "%~dp0Folder2.jpg" "%%I\"
    )
    copy /Y "%~dp0aGallery-dl.bat" "%%I\"
)
endlocal

如果包含空格或以下字符之一,则必须将文件/文件夹名称包含在"中.因此,所有文件/文件夹名称都用双引号括在该批处理文件中,尽管在批处理文件中没有文件/文件夹名称包含空格或列表中的字符之一.在批处理文件编写过程中了解Windows命令行处理器处理命令行后的命令行外观非常重要.请参阅以下主题:

Windows解释*.*就像*一样,表示任何文件或文件夹的名称.因此,只需编写*并省略.*.

请注意,for /D会忽略设置了隐藏属性的目录.

该批处理文件首先检查每个子文件夹是否不包含批处理文件aGallery-dl.bat.在这种情况下,它将两个文件Folder.jpgFolder2.jpg从已执行批处理文件的目录复制到U:\11Web\gallery-dl\deviantart的当前子目录.

然后将批处理文件aGallery-dl.bat从执行的批处理文件目录复制到U:\11Web\gallery-dl\deviantart的当前子目录,而与它在目标目录中的存在无关.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,然后在其中执行按照以下命令操作,并非常仔细地阅读每个命令显示的所有帮助页面.

  • call /? ...说明%~dp0 ...驱动器和参数0的路径...完整批处理文件路径.
  • cmd /? ...输出Windows命令处理器执行批处理文件的帮助.
  • copy /?
  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • setlocal /?

另请参见此答案为什么默认情况下没有必要使用setlocal EnableExtensions DisableDelayedExpansionendlocal,为什么使用I代替a作为循环变量,尽管a也可以在这里工作.

Morning all.

So I've been up hours trying to cobble together -a variety of replies to other posts- into my own code in order to see if I could get something usable. No-go. I'm sufficiently lost in the sauce that I've now got to ask for some help from you.

Background: OS: Windows 10 I use the program text2folders.exe to create 20-30 new folders on a secondary drive every night. Primarily, I have a base file "aGallery-dl.bat" that I populate each folder with using an xcopy batch file. Secondarily, from time to time I update the source file "aGallery-dl.bat" using the same xcopy and this overwrites the older target file, populating all folders with the newest "aGallery-dl.bat" (whether they need it or not). All is well.

@echo off
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy /y /d ".\aGallery-dl.bat" "%%a\"

I've recently decided I want to add two new files to each folder and have expanded my xcopy to include these. All is well.

@echo off
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy /y /d ".\aGallery-dl.bat" "%%a\"
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy ".\Folder.jpg" "%%a\"
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy ".\Folder2.jpg" "%%a\"

Folder.jpg a big red X

Folder2.jpg a big yellow ! mark

When I choose to run a "aGallery-dl.bat" in a given folder (again, one of 100's), it first deletes Folder.jpg then renames Folder2.jpg to Folder.jpg. This has the effect of the red X being replaced by the yellow ! when viewing the folder in File Explorer parent folder. Secondly, it calls "gallery-dl.exe." I use going from red to yellow to let me know I've run "aGallery-dl.bat" at least once. All is well.

rem @echo off
del .\Folder.jpg
ren .\Folder2.jpg Folder.jpg
FOR /F %%i IN ('cd') DO set FOLDER=%%~nxi
"C:\Program Files (x86)\gallery-dl\gallery-dl.exe" -d "U:\11Web\gallery-dl" --download-archive ".\aGDB.sqlite3" "https://www.deviantart.com/"%FOLDER%"/gallery/all"
del .\Folder.jpg

If "aGallery-dl.bat" completes successfully, it finally deletes the Folder.jpg (currently yellow !), and now the representative contents of the folder (usually DeviantArt .jpg's) are visible.

Problem: When I have to re-run my original xcopy command to update "aGallery-dl.bat" in ALL FOLDERS, the Folder.jpg and Folder2.jpg will be re-copied to all folders, defeating the purpose of deleting them once via "aGallery-dl.bat." I don't want to have to go back and re-run "aGallery-dl.bat" intermittently across 100's of folders (again, only those that have had aGallery-dl.bat run at least once). I need some type of test, that if "aGallery-dl.bat" is already present in the target folder, DO NOT xcopy Folder.jpg and Folder2.jpg aka vague example, below.

*********************************Some sort of test statement here!!!***********************

:aGallery-dlPresent
GOTO eof

:aGallery-dlNotPresent
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy ".\Folder.jpg" "%%a\"
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy ".\Folder2.jpg" "%%a\"
GOTO eof

:eof

I had found a hopeful candidate test statement in the below (copied in its original form from what/where I read in other post), but am looking for ideas/replacements as I HAVE NO IDEA how to modify/inject/implement the below to work in the above.

If exist \\%DIR%\%Folder%\123456789.wav xcopy \\%DIR%\%Folder%\123456789.wav D:\%New Folder%\ /y

Having XCopy copy a file and not overwrite the previous one if it exists (without prompting)

Note: The below is a vague approximation of what it should all look like (barring having a correct -test statement-).

rem @echo off
*********************************Some sort of test statement here!!!***********************

:aGallery-dlPresent
GOTO eof

:aGallery-dlNotPresent
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy ".\Folder.jpg" "%%a\"
for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy ".\Folder2.jpg" "%%a\"
GOTO eof

:eof

for /D %%a in ("U:\11Web\gallery-dl\deviantart\*.*") do xcopy /y /d ".\aGallery-dl.bat" "%%a\"

解决方案

The command for copying a file is COPY. It is an internal command of Windows command processor cmd.exe. XCOPY is an eXtended file and directory copying executable in directory %SystemRoot%\System32 which is deprecated since Windows Vista as there is even more powerful ROBOCOPY which is with full qualified file name %SystemRoot%\System32\robocopy.exe.

There is no need to use XCOPY or ROBOCOPY for this simple file copying task. COPY is enough on source files aGallery-dl.bat, Folder.jpgand Folder2.jpg don't have hidden attribute set and the same files in the target directories don't have read-only attribute set.

.\ references the current directory which can be any directory. Windows Explorer sets the directory of the batch file as current directory on double clicking on a batch file. But this is nearly the only method to run a batch file on which the directory of the executed batch file is set automatically as current directory (except the batch file is stored on a network resource accessed using UNC path).

There is %~dp0 to reference the path of the batch file. This path always ends with a backslash which means that no additional backslash is needed on concatenating the batch file path with a file or folder name. The usage of %~dp0 makes it possible to reference files in same directory as the executed batch file independent on which directory is the current directory on execution of the batch file.

The batch file needed for your task is:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /D %%I in ("U:\11Web\gallery-dl\deviantart\*") do (
    if not exist "%%I\aGallery-dl.bat" (
        copy "%~dp0Folder.jpg" "%%I\"
        copy "%~dp0Folder2.jpg" "%%I\"
    )
    copy /Y "%~dp0aGallery-dl.bat" "%%I\"
)
endlocal

A file/folder name must be enclosed in " if containing a space or one of these characters &()[]{}^=;!'+,`~. For that reason all file/folder names are enclosed in this batch file in double quotes although inside the batch files no file/folder name contains a space or one of the characters in the list. It is important to understand on batch file writing how a command line looks like after Windows command processor processed the command line. See following topics:

Windows interprets *.* like just * which means any file or folder name. For that reason it is enough to just write * and omit .*.

Please note that for /D ignores directories with hidden attribute set.

The batch file checks first for each subfolder if it does not contain the batch file aGallery-dl.bat. In this case it copies the two files Folder.jpg and Folder2.jpg from directory of executed batch file to current subdirectory of U:\11Web\gallery-dl\deviantart.

Then the batch file aGallery-dl.bat is copied from directory of executed batch file to to current subdirectory of U:\11Web\gallery-dl\deviantart independent on its existence in the destination directory.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /? ... explains %~dp0 ... drive and path of argument 0 ... full batch file path.
  • cmd /? ... outputs the help of Windows command processor executing a batch file.
  • copy /?
  • echo /?
  • endlocal /?
  • for /?
  • if /?
  • setlocal /?

See also the chapters Issue 6 and Issue 7 in this answer why using setlocal EnableExtensions DisableDelayedExpansion and endlocal although not necessary by default and why using I instead of a as loop variable although a would work here, too.

这篇关于测试IF文件是否存在,ELSE xcopy这两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆