在批处理文件编程中使用For Loop移动文件

时间:2019-04-18 08:19:22

标签: batch-file

我正在尝试创建一个来按文件类型组织文件。

这个想法是将所有带有扩展名的文件(除了运行脚本之外的文件)移到新目录中,每个目录都以扩展名命名。

我需要添加或替换下面给出的某些代码的什么代码?该.bat文件位于Documents文件夹中,并且那里有一些文件。

@echo off
set x="%cd%\log.txt"
rem 'log.txt' contains the name of folder 'My Folder' and is already created in Downloads folder.
for %%a in (".\*") do (
    for /F "tokens=*" %%i In ('type %x%') do (
        if "%%~xa" neq "" if "%%~dpnxa" neq "%~dpnx0" (
            if not exist "C:\Users\%username%\Downloads\%%i\%%~xa" md "C:\Users\%username%\Downloads\%%i\%%~xa"
            move "%%a" "C:\Users\%username%\Downloads\%%i\%%~xa\"
        )
    )
)
pause

输出:

The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
Press any key to continue . . .

2 个答案:

答案 0 :(得分:0)

这将以netG.load_state_dict(torch.load('generator.pth')['model_state_dict']) 的形式创建包含点的文件夹。

C:\users\username\Downloads\.ext

如果您不想要该点。即@echo off set "x=%~dp0\log.txt" rem 'log.txt' contains the name of folder 'My Folder' and is already created in Downloads folder. for /F "delims=" %%i In ('type %x%') do ( if "%%~xi" neq "" if "%%~fi" neq "%~f0" ( md "%userprofile%\%%~xi" >nul 2>&1 move "%%~fi" "%userprofile%\Downloads\%%~xi\" ) ) pause

C:\users\username\Downloads\ext

请注意,我从@echo off setlocal enabledelayedexpansion set "x=%~dp0\log.txt" rem 'log.txt' contains the name of folder 'My Folder' and is already created in Downloads folder. for /F "delims=" %%i In ('type %x%') do ( if "%%~xi" neq "" if "%%~fi" neq "%~f0" ( set "ext=%%~xi" call set ext=!ext:.=! md "%userprofile%\!ext!" >nul 2>&1 echo move "%%~fi" "%userprofile%\Downloads\!ext!" ) ) pause 命令中删除了%%i,因为它会创建一个带有文件名的文件夹,如果需要的话,只需将其放回去。

答案 1 :(得分:0)

鉴于我对您要执行的操作的了解,我建议使用类似的方法:

@Echo Off
Set "DestDirName=My Folder"
If Exist "%~dp0log.txt" Set /P "DestDirName="<"%~dp0log.txt"
If Exist "log.txt" Set /P "DestDirName="<"log.txt"
For %%A In (*)Do If /I Not "%%A"=="%~nx0" If Not "%%A"=="log.txt" If Not "%%~xA"=="" (
    Set "FileExtension=%%~xA"
    SetLocal EnableDelayedExpansion
    RoboCopy . "%UserProfile%\Downloads\%DestDirName%\!FileExtension:~1!" "%%A" /Mov>Nul
    EndLocal
)
Pause

234Set Downloads中父目录的名称,该目录将保存要移动文件的目录至。我已使用名为My Folder的新目录作为备份,以防万一在当前目录或保存您正在运行的批处理文件的目录中找不到文件log.txt。当前,它按照以下优先顺序选择名称:“当前目录”,“批处理文件目录”,“我的文件夹”;如果希望“批处理文件目录”优先于“当前”目录,则可以切换行34。如果找到log.txt,则Set /P命令会将变量DestDirName设置为该文件第一行的内容,请请勿在其中使用双引号或尾部反斜杠。该内容

然后,For循环将继续从目录行2将所有带有扩展名的文件移动到目录中以扩展名 命名的目录中。 -4,并在用户Downloads目录中。这样做时,它将不包含自身或任何名为log.txt的文件。

顺便说一句,%DestDirName%中是否已经存在目录名字符串值Downloads并不重要,如果没有,它将自动创建。。 / p>