如何找到文件夹的路径并将文件放入其中?

时间:2014-08-20 04:11:44

标签: batch-file cmd path-finding

我想写一个.bat脚本,它找到已安装程序的文件夹(例如Acrobat \ JavaScript),因为该文件夹在每台计算机上都可以有不同的路径。然后在里面放一个.js文件和一个imgfolder。 .js文件和imgfolder与.bat脚本位于同一文件夹中。如何在驱动器C上找到Acrobat \ JavaScript的完整路径并保存到变量以便复制.js内部?

以下是我的尝试:

@echo off

REM  find the folder with my .bat file 

for /f %%i in ("%0") do set curpath=%%~dpi 
echo  %curpath% 

REM it fails on this place:

for /R "C:\Programm Files(x86)" /D %d in (*) 
  do @if "%~nd" == "Acrobat\JavaScript" 

REM  I want to save here the path "%d" to a variable  in order to use it for copying

echo "%d"  

pause

如果有效,我将使用它将imgfolder和.js文件复制到检索到的路径:

XCOPY %curpath%\imgFolder %d

复制文件夹:

XCOPY %curpath%\script.js %d /i

更新:

我在注册表中查了一下,我找到了HKEY_LOCAL_MACHINE \ SOFTWARE \ Wow6432Node \ Adob​​e Acrobat \ 然后是所有版本9.0 - 11.0。据我所知,路径保存在InstallPath文件夹中,因此我合并了:

 "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\Acrobat.exe" /v InstallPath') ... 

是不是?

 set path=""

 for /f "tokens=1-2*" %%A in ('reg query   "HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\Acrobat.exe" /v InstallPath') do (
 set path="%%C\JavaScript"
 )

echo path
pause

Error:
W:\batScript>for /F "tokens=1-2*" %A in ('reg query "HKLM\Software
\Microsoft\Windows\CurrentVersion\App Paths\Acrobat.exe" /v InstallPath') do (se
t path="%C\VBoxManage.ex" )
'reg' is not recognized as an internal or external command, operable program or batch file.

我试过用reg.exe查询,同样的错误。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的代码中出现错误'reg' is not recognized as an internal or external command, operable program or batch file.,因为您已使用代码path覆盖了系统set path=""。切勿使用名称为path的变量,因为它会弄乱您的默认系统路径。

尝试使用这段代码来识别adobe路径。

@echo off
for /f "skip=2 tokens=*" %%i in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRd32.exe" /V path  2^>nul') do  (set "adobe_path=%%i")
set adobe_path=%adobe_path:path    REG_SZ    =%
echo %adobe_path%

测试输出 -

D:\Scripts>type op.bat
@echo off
for /f "skip=2 tokens=*" %%i in ('reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRd32.exe" /V path  2^>nul') do  (set "adobe_path=%%i")
set adobe_path=%adobe_path:path    REG_SZ    =%
echo %adobe_path%
D:\Scripts>
D:\Scripts>
D:\Scripts>op.bat
C:\Program Files (x86)\Adobe\Reader 11.0\Reader\

D:\Scripts>

干杯, ģ