我需要将一个文本文件中命名的文件移动到另一个不同文本文件中命名的目录。使用批处理命令。
我尝试了什么。
@echo off
echo.
REM check if file is there
if exist K:\file_sync_diff\FileNameList.txt goto Label 1
REM display error
echo Can not find the File Name List
echo.
echo.
Pause
goto :eof
:Label 1
REM display that the file in the last check was found
echo found FileNameList.txt
REM check if file is there
if exist K:\file_sync_diff\FileDumpText.txt goto Label 2
REM display error
echo Can not find File Dump Text File
echo.
echo.
Pause
goto :eof
:Label 2
REM display that the file in the last check was found
echo found FileDumpText.txt
REM check if file is there
if exist K:\file_sync_diff\DirectoryNames.txt goto Label 3
REM display error
echo Can not find Directory Names Text File
echo.
echo.
Pause
goto :eof
:Label 3
REM display that the file in the last check was found
echo found DirectoryNames.txt
REM for loop to filter through every line in a file
echo.
for /f %%i in (K:\file_sync_diff\FileNameList.txt) do call :Sub %%i
goto Label 4
goto :eof
:Label 4
REM display message of the file being moved
echo.
echo Moving %1
REM copy the file just made to a directory with a name supplied in a text file
for /f %%i in (K:\file_sync_diff\DirectoryNames.txt) do call :Sub 2 %%i
echo.
goto :eof
:Sub
echo Writing %1
REM copy the contents of FileDumpText.txt to the file that was passed in the last method
type K:\file_sync_diff\FileDumpText.txt >> %1.txt
goto :eof
:Sub 2
REM moves the file to the directory supplied by label 4.
move /y %1.txt %1
echo.
goto :eof
FileNameList.txt
red
orange
purple
DirectoryNames.txt
K:\file_sync_diff\cat
K:\file_sync_diff\dog
K:\file_sync_diff\333
不重要但是FileDumpText.txt
的内容
测试要传递给文件的文本
more text 1
more text 2
more text 3
目录确实存在于K:\file_sync_diff
文件夹中。
感谢您的帮助。
最终结果应如下 目录cat里面有red.txt,里面有FileDumpText.txt的所有内容 目录dog里面有orange.txt,里面有FileDumpText.txt的所有内容 目录333里面有purple.txt,里面有FileDumpText.txt的所有内容
答案 0 :(得分:0)
我对associative arrays
的建议:
@ECHO OFF &SETLOCAL
set "tfileA=K:\file_sync_diff\FileNameList.txt"
set "tfileB=K:\file_sync_diff\DirectoryNames.txt"
set "tfileC=K:\file_sync_diff\FileDumpText.txt"
for /f "tokens=1*delims=:" %%a in ('findstr /n $ "%tfileA%"') do set "$a%%a=%%b"&set /a countA=%%a
for /f "tokens=1*delims=:" %%a in ('findstr /n $ "%tfileB%"') do set "$b%%a=%%b"&set /a countB=%%a
for /f "tokens=1*delims=:" %%a in ('findstr /n $ "%tfileC%"') do set "$c%%a=%%b"&set /a countC=%%a
if "%countA%"=="%countB%" (echo %countA% line(s^) found in %tfileA% and %tfileB%.) else echo Line mismatch: %tfileA%:%countA% - %tfileB%:%countB%&goto:eof
if "%countA%"=="%countC%" (echo %countA% line(s^) found in %tfileC%.) else echo Line mismatch: %tfileA%:%countA% - %tfileC%:%countC%&goto:eof
SETLOCAL ENABLEDELAYEDEXPANSION
for /l %%a in (1 1 %countA%) do (
echo copy "!$a%%a!" "!$b%%a!"
echo ^>"!$b%%a!\!$a%%a!" echo(!$c%%a!
)
endlocal
输出是:
3 line(s) found in K:\file_sync_diff\FileNameList.txt and K:\file_sync_diff\DirectoryNames.txt. 3 line(s) found in K:\file_sync_diff\FileDumpText.txt. copy "red" "K:\file_sync_diff\cat" >"K:\file_sync_diff\cat\red" echo(more text 1 copy "orange" "K:\file_sync_diff\dog" >"K:\file_sync_diff\dog\orange" echo(more text 2 copy "purple" "K:\file_sync_diff\333" >"K:\file_sync_diff\333\purple" echo(more text 3
答案 1 :(得分:0)
我成功使用以下代码,但有一个问题。我现在得到一个“.txt”文件。这是由FileNameList.txt文件中的返回包机引起的,但如果它们在FileNameList.txt中的最后一个文件名后没有返回,则该文件不会被复制。 所以我只需要一行代码来删除“.txt”而不是任何其他具有实际名称的文件.txt任何帮助都会很好。
我目前的代码
@echo off
echo.
REM check if file is there
if exist F:\file_sync_diff\FileNameList.txt goto Label 1
REM display error
echo Can not find the File Name List
echo.
echo.
Pause
goto :eof
:Label 1
REM display that the file in the last check was found
echo found FileNameList.txt
REM check if file is there
if exist F:\file_sync_diff\FileDumpText.txt goto Label 2
REM display error
echo Can not find File Dump Text File
echo.
echo.
Pause
goto :eof
:Label 2
REM display that the file in the last check was found
echo found FileDumpText.txt
REM check if file is there
if exist F:\file_sync_diff\DirectoryNames.txt goto Label 3
REM display error
echo Can not find Directory Names Text File
echo.
echo.
Pause
goto :eof
:Label 3
REM display that the file in the last check was found
echo found DirectoryNames.txt
REM for loop to filter through every line in a file
echo.
for /f %%i in (F:\file_sync_diff\FileNameList.txt) do call :Sub %%i
goto Label 4
goto :eof
:Label 4
REM thanks to Endoro@StackOverFlow
@ECHO OFF &SETLOCAL
REM set associative arrays
set "tfileA=F:\file_sync_diff\FileNameList.txt"
set "tfileB=F:\file_sync_diff\DirectoryNames.txt"
REM setup for loops
for /f "tokens=1*delims=:" %%a in ('findstr /n $ "%tfileA%"') do set "$a%%a=%%b"&set /a countA=%%a
for /f "tokens=1*delims=:" %%a in ('findstr /n $ "%tfileB%"') do set "$b%%a=%%b"&set /a countB=%%a
SETLOCAL ENABLEDELAYEDEXPANSION
for /l %%a in (1 1 %countA%) do (
echo.
move "!$a%%a!.txt" "!$b%%a!"
echo.
echo ^>"!$b%%a!\!$a%%a!" echo(!$c%%a!)
echo.
endlocal
:Sub
echo Writing %1
REM copy the contents of FileDumpText.txt to the file that was passed in the last method
type F:\file_sync_diff\FileDumpText.txt >> %1.txt
goto :eof