这是当前代码:
@ECHO OFF
ECHO.
REM rename
REN "H:\DIRECTORY with Space\folder1\*.*" "H:\DIRECTORY with Space\Folder1\TEST_*.*"
ECHO.
DIR nofile || (PAUSE && EXIT /B 1)
我也尝试过move / y
move /y "H:\DIRECTORY with Space\folder1\*.*" "H:\DIRECTORY with Space\folder1\TEST_*.*"
两者都不起作用(找不到语法错误或目录)。基本上是尝试使用前缀(“ Test_”)重命名子文件夹中的所有内容,并覆盖所有重复项。
答案 0 :(得分:1)
尝试一下。我对代码进行了注释,以帮助更好地解释其工作原理。
@ECHO OFF
:: Changes to a directory and saves previous directory to go back to
PUSHD "H:\DIRECTORY with Space\folder1\"
:: Critical that you USE this instead of a normal FOR command.
:: A normal FOR command may attempt to rename a file twice.
FOR /F "delims=" %%G IN ('DIR /a-d /b *.*') do rename "%%~G" "TEST_%%~G"
:: Goes back to the previous directory.
POPD
答案 1 :(得分:0)
很高兴,Squashman的回答似乎比我更好。
只需添加一些答案即可。
(Sorry I can't comment.)
在FOR /F "delims=" %%G IN ('DIR /a-d /b *.*') do ren "%%~G" "TEST_%%~G"
添加此项可以帮助重命名目录中的FOLDER(S)。 FOR /F "delims=" %%G IN ('DIR /ad /b *.*') do ren "%%~G" "TEST_%%~G"
但是我建议使用:
for /f "delims=" %%a in ('DIR /a-d /s /b ^| findstr /v /c:"%~nx0") do (ren "%%a" "TEST_%%~nxa" >nul 2>nul )
for /f "delims=" %%a in ('DIR /ad /s /b ^| sort /r') do (ren "%%a" "TEST_%%~nxa" >nul 2>nul )
还有更多扩展功能:
@echo off
rem not necessary
title to whom it may rename
color 0a
mode 120,25
goto start
rem is necessary
:start
cls
echo Please enter the path which you want to rename all contents in it with a prefix. No need for the quotes.
echo %%cd%% for the current working directory
set /p p=The path:
call set p=%p:"=%
echo.
if not exist "%p:"=%" (echo The folder doesn't exist in the typed in path.
echo Current working directory: "%cd%"
echo Please try another path. Press any key to continue.
pause >nul
goto start)
rem adding Squashman's answers here
pushd "%p%"
echo Renaming file(s) in the required folder.
for /f "delims=" %%a in ('DIR /a-d /s /b ^| findstr /v /c:"%~nx0") do (ren "%%a" "TEST_%%~nxa" >nul 2>nul )
echo Renaming (sub)folder(s) in the required folder.
for /f "delims=" %%a in ('DIR /ad /s /b ^| sort /r') do (ren "%%a" "TEST_%%~nxa" >nul 2>nul )
echo.
if "%cd%\" == "%~dp0" (echo Do you want to rename this batch too? It will auto exit after renaming.
choice /c yn /n /m "[Y/N] ")
if "%errorlevel%" == "1" (ren "%~nx0" "TEST_%~nx0" >nul 2>nul )
echo Renaming finished. Please press any key to leave the batch.
pause >nul
exit