简体   繁体   English

如何使用批处理脚本从当前目录复制文件,而不复制子目录?

[英]How do I copy files from the current directory, but not sub-directories using a batch script?

I have a school assignment and I am stuck on this one question. 我有一份学校作业,我坚持这个问题。 I have no idea where else to turn. 我不知道还能在哪里转。

So this question follows on from the previous question which is to make a script to copy "myfile.txt" to my environment variable %BackUpPath% (which is set to C:\\backup). 所以这个问题是从上一个问题开始的,即将脚本“myfile.txt”复制到我的环境变量%BackUpPath%(设置为C:\\ backup)。 My script is as follows: 我的脚本如下:

  • copy /y myfile.txt %backuppath% copy / y myfile.txt%backuppath%

The question I'm stuck on asks me to make a script using an IF EXIST statement in conjunction with a FOR loop to copy all the files in the current directory, but not any sub-directories to %backuppath%. 我坚持的问题要求我使用IF EXIST语句结合FOR循环创建一个脚本,以复制当前目录中的所有文件,但不复制任何子目录到%backuppath%。

How should I write this script? 我该怎么写这个脚本?

按顺序尝试这些命令

@echo off tree "C:\\backup" find /c "*.TXT" C:\\Backup if exists "file path\\name" move /y "files within the folder to other folder" del /f /s "main file-path" for each %.txt IN C:\\Backup goto a <replace move with copy if needed> ;;this is a rough idea of what you might need.

This should solve the question, but it's academic rather than a real life code. 这应该可以解决问题,但它是学术性的,而不是现实生活中的代码。

It uses the recursive switch of for-in-do and checks if the filename.ext that is generated by the for-in-do actually exists in the current directory - and then copies those files. 它使用for-in-do的递归开关并检查由for-in-do生成的filename.ext是否实际存在于当前目录中 - 然后复制这些文件。

@echo off
set "backuppath=c:\folder"
for /r %%a in (*) do (
    if exist "%cd%\%%~nxa" (
       echo copying "%%a"
       copy "%%a" "%backuppath%" >nul
    )
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将文件批量复制到多个子目录? - how can I batch copy a file into multiple sub-directories? 如何使用PowerShell为多个子目录创建目录连接? - How to create directory junctions for multiple sub-directories using PowerShell? 如何删除子目录中的文件和文件夹,但在Ruby中跳过某些文件名的删除? - How do I delete files and folders in sub-directories but skip delete for certain file names in Ruby? ImageMagick 的批处理命令在 Windows 上转换目录和子目录中的所有文件 - Batch command for ImageMagick to convert all files in a directory and sub-directories on windows 如何使用批处理文件将文件从父文件夹复制到子文件夹? - How do I copy files from parent folder to sub folders using batch file? 仅在目录,子目录中读取最近2个月的文件 - Read files for last 2 months only in directory, sub-directories 尝试使用Bat文件获取目录和子目录中所有文件的行数 - Trying to Get A Line Count for all Files in a Directory and Sub-Directories using a Bat File 如何使用 Python 遍历包含子目录的目录中的文件? - How do I iterate over files in a directory including sub directories, using Python? 批处理命令将文件从子目录移动到新目录 - Batch Command to Move Files from Sub Directories to New Directory 使用批处理脚本的当前目录中文件,文件夹和子文件夹的总数 - Total count of files,folders and sub folders in the current directory using batch script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM