简体   繁体   English

批处理文件循环遍历目录,并基于每个文件创建变量

[英]Batch file to loop through a directory and create a variable based on each file

I have a text file with a list of server IP addresses and the code below (which I've scrapped together from other coding) loops through it and brings back a modified date of a named file for each server in the list... 我有一个带有服务器IP地址列表的文本文件,下面的代码(我从其他编码中删除了这些代码)循环遍历它,并为列表中的每个服务器带回一个命名文件的修改日期...

@ECHO On
SETLOCAL
FOR /f %%a IN (C:\Scripts\Servers.txt) DO (
 CALL :getmod %%a
)

GOTO :EOF

:getmod
SET Server=%1
SET File=Abs_Client.exe

FOR %%i IN ("\\%Server%\C$\Com_Dir\%File%") DO SET modif_time=%%~ti
Echo %Server% %File% %modif_time% >> "C:\Scripts\Server_App_Mod_date.txt"

GOTO :eof

That works great...however, what I'd like to do is create another loop around it which creates a variable for each file in a directory and pass that into the code above instead of having to manually change the 'SET File' as shown above for individual files. 效果很好...但是,我想做的是围绕它创建另一个循环,该循环为目录中的每个文件创建一个变量,并将其传递到上面的代码中,而不必手动更改“ SET File”为上面显示的是单个文件。

Something along the lines of; 某种东西

@ECHO On
SETLOCAL

FOR /D %VAR IN ("\\Network_Location\AppMedia\App Source Files\Prod Apps\Server_Update") DO (

FOR /f %%a IN (C:\Scripts\Servers.txt) DO (
 CALL :getmod %%a
)

GOTO :EOF

:getmod
SET Server=%1
SET File=%VAR

FOR %%i IN ("\\%Server%\C$\Com_Dir\%File%") DO SET modif_time=%%~ti
Echo %Server% %File% %modif_time% >> "C:\Scripts\Server_App_Mod_date.txt"

GOTO :eof

)

Clearly it's wrong so any ideas/help please? 显然这是错误的,所以有什么想法/帮助吗?

haven't testet, but maybe a hint in the right direction: 还没有睾丸,但可能是正确方向的提示:

@ECHO ON
SETLOCAL 

FOR /F "TOKENS=*" %%F IN ('DIR "\\Network_Location\AppMedia\App Source Files\Prod Apps\Server_Update" /s /b /a:-d') DO (
  FOR /F %%A IN (C:\Scripts\Servers.txt) DO (
    CALL :getmod %%A "%%~nxF"
  )
)

GOTO :EOF

:getmod
SET Server=%1
SET "tmpFile=%~2"

FOR %%I IN ("\\%Server%\C$\Com_Dir\%tmpFile%") DO ECHO %Server% %tmpFile% %%~tI >> "C:\Scripts\Server_App_Mod_date.txt"
GOTO :EOF

As far as i know, FOR /D only executes for directorys and if i understand your question, you have files in "Prod Apps\\Server_Update", for each you like to have the file-date/time from the target-server... right? 据我所知,FOR / D仅对目录执行,如果我理解您的问题,则您在“ Prod Apps \\ Server_Update”中都有文件,每个文件都希望从目标服务器获取文件日期/时间。 。 对?

Edit: 编辑:

Maybe this works too: 也许这也可行:

FOR /F "TOKENS=*" %%F IN ('DIR "\\Network_Location\AppMedia\App Source Files\Prod Apps\Server_Update" /s /b /a:-d') DO (
  FOR /F %%A IN (C:\Scripts\Servers.txt) DO (
    FOR %%X IN ("\\%%A\C$\Com_Dir\%%~nxF") DO ECHO %%A %%~nxF %%~tX >> "C:\Scripts\Server_App_Mod_date.txt"
  )
)

without the :getmod 没有:getmod

Edit: /b-switch was missing from the first DIR-Command in 2nd suggestion 编辑:/ b开关从第二个建议中的第一个DIR命令中丢失

@ECHO On
SETLOCAL
FOR /f %%a IN (C:\Scripts\Servers.txt) DO (
 FOR /f "delims=" %%i IN ('dir /b/a-d "\\%%a\C$\Com_Dir\*"') DO Echo %%a %%i %%~ti >> "C:\Scripts\Server_App_Mod_date.txt"
)
GOTO :EOF

Should work, IIUC. 应该工作,IIUC。 Can't test, I'm afraid... 无法测试,恐怕...

[Edit - removed call to getmod - not required] [编辑-删除对getmod调用-不需要]

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

相关问题 批处理文件遍历目录? - Batch file loop through a directory? 批处理文件循环遍历目录中的每个文件并计数每个文件中的记录 - Batch File to Loop Through Each File in Directory and Count Records in Each File 如何通过批处理文件基于可变长度文件名创建文件夹 - How to create folders based on variable length filenames through batch file 使用批处理文件循环目录名称? - loop through directory names using a batch file? 批处理文件循环通过远程 Git 分支并将每个分支中的类似 CSV 文件复制到本地目录 - Batch File to Loop Through Remote Git Branches and Copy a Similar CSV File in Each Branch to a Local Directory 根据文件名和目录创建文本文件 - 批处理 - create text file based on filenames and directory - batch 批处理文件以运行文本文件并根据每一行创建IP地址 - Batch file to run through a text file and create an IP addesss based on each line 知道如何在批处理文件中循环遍历数组,根据每个值获取信息吗? - Any idea how I can loop through an array, in a batch file, getting info based on each value? 批量循环文件 - Batch loop through file 批处理脚本 - 遍历文本文件并将每一行设置为变量并运行脚本,然后重复下一行 - Batch Script - Loop through text file and set each line as variable and run script then repeat for next line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM