每次放置新文件时,在批处理文件中获取文件名

时间:2015-02-20 15:40:55

标签: batch-file

我的要求是 - 我需要从输入文件夹中读取文件名 - C:\Encrypt\In并将其传递给命令java.exe -jar D:\ SYS \ src \ PI \ IN \ Cryptage.jar - rc4 -crypt D:\ SYS \ src \ PI \ IN \ Decrypt \ D:\ src \ PI \ IN \ Encrypt \%VAR1 %% VAR2%

我尝试过以下一个 - 但没有运气

set VAR1=FOR /R C:\Encrypt\In %F in (*.*) do echo %~nF
set VAR2=ABCD

echo %VAR1%%VAR2% (concatenate the filename with "ABCD" as constant)


java.exe -jar D:\SYS\src\PI\IN\Cryptage.jar -rc4 -crypt D:\SYS\src\PI\IN\Decrypt\ D:\src\PI\IN\Encrypt\ %VAR1%%VAR2% 

(在这里传递 - 这样每次文件进入输入目录时,变量都可以通过变量动态获取文件名)

echo %VAR1%%VAR2%无效。

2 个答案:

答案 0 :(得分:0)

以下是您的任务的注释批处理代码:

@echo off
set "ScanFolder=C:\Encrypt\In"

rem The loop runs command DIR to get a list of files with archive attribute set.
rem Directories are ignored even if archive attribute is set on a directory.
rem On each file with archive attribute currently set the archive attribute
rem is removed from file and then the command is started to process the file.
rem After all files with archive attribute were processed, the batch file
rem waits 5 seconds before scanning the folder again. The loop is infinite
rem and can be breaked only by pressing Ctrl+C or closing command prompt
rem window to stop command line interpreter.

:Loop
for /F "delims=" %%F in ('dir /AA-D /B "%ScanFolder%" 2^>nul') do (
    %SystemRoot%\System32\attrib.exe -A "%ScanFolder%\%%~nxF"
    java.exe -jar D:\SYS\src\PI\IN\Cryptage.jar -rc4 -crypt D:\SYS\src\PI\IN\Decrypt\ D:\src\PI\IN\Encrypt\ "%ScanFolder%\%%~nxF"
)
%SystemRoot%\System32\ping.exe -n 6 127.0.0.1>nul
goto Loop
如果可能的话,应该使用双引号用完整路径调用

java.exe,因为在这种情况下,命令行解释器并不总是需要在环境变量 PATH 的文件夹中搜索它。

注意:批处理文件使用完整路径,文件名和扩展名调用新文件,而不添加任何内容。当然,如果您的环境中有必要,也可以在%%~nxF的行尾调用java.exe替换%%~nFABCD

有关已使用命令及其工作原理的详细说明,请打开命令提示符窗口并执行以下命令以查看这些命令的帮助:

  • attrib /?
  • dir /?
  • for /?
  • ping /?

答案 1 :(得分:0)

非常感谢 - 我通过以下方式实现了: - cd C:\ Encrypt \ In \ for %% f in(。)do(rem echo %% ~nfAPSI set v = %% ~nfAPSI)echo%v%< / p>

相关问题