我有一個這樣的文件/目錄結構:的Windows批處理文件,其曾祖目錄中複製文件添加爲前綴爲filename
C:\Results\name1\run\string1\output.csv
C:\Results\name2\run\string2\output.csv
C:\Results\name3\run\string3\output.csv
...
我想複製每個output.csv
到C:\Results
其巨大-grandparent目錄名稱作爲前綴添加到文件,所以我有這樣的:
C:\Results\name1output.csv
C:\Results\name2output.csv
C:\Results\name3output.csv
我嘗試使用批處理代碼貼在下面,但它不能令人信服。
(我試圖從其他網上搜索湊齊了這一點。)
@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ('dir /A:D /S /B "C:\Results*"') do (
for %%y in ("%%a\*.csv") do (
call :GETPARENTPARENT "%%y" ret
echo ret=!ret!
move /Y "%%y" "!ret!"
pause
)
)
goto :EOF
:GETPARENTPARENT
set fileP=%1
echo received=%fileP%
for %%a in (%fileP%) do (
set parent=%%~dpa
cd !parent!\..\..
for /r %%a in (*.csv) do for %%b in ("%%~dpa\.")
do ren "%%~a" "%%~nxb%%~xa"
set gparent=%%~dpa
cd !gparent!\..
set PPPath=!cd!
for %%x in ("!PPPath!") do (
set "%~2=%%~dpnx"
)
)
goto :EOF
Cmd.exe shell腳本(批處理文件)與DOS沒有任何關係。 –
「不完全在那裏」是什麼意思?你的代碼崩潰了嗎? (因爲'do'必須與'for'在同一條物理線上),或者根本不按預期工作(這可能與您嘗試使用* same * metavariable嵌套兩個for語句有關, %a') – Magoo
[DOS和cmd不同](https://superuser.com/q/451432/241386)。除非腳本應該在DOS下運行,否則不應該標記DOS –