我有一个批处理文件正在搜索文件,复制它们然后压缩。问题区域在最后,我在批处理文件中创建一个vbs文件,然后运行它。这是我的代码:
@echo off
setlocal enabledelayedexpansion
REM ** Loops through each arugment in the input file
set fileList=sys-conf.txt, CNCIDNUM.txt, cnc-para.txt,pitch.txt,OPRT_HIS.txt,TOOLOFST.TXT,macro.txt,3D-INTER.txt,ATCMagazine_,Makino_AtdAxis_CMN_,Makino_AtdAxis_Duty_,Makino_Atdspindle_,Makino_IOlink_setting,Makino_MMS_,Makino_Parameter_,Makino_QAC_CurveFit_,Makino_QAC_Override_,Makino_QAC_Setting_,PalletMagazine_,MCA_selectData.txt
:getDestination
set /p destination=Enter the destination Folder to copy the files :
if not exist !destination! goto getDestination
for %%A in ("%~f0\..") do set "fol=%%~nxA"
set m_dd=%date%_%time%
set m_dd=%m_dd: =%
set m_dd=%m_dd::=_%
set m_dd=%m_dd:/=_%
set m_dd=%m_dd:.=_%
set m_d=%m_dd:~3,-3%
set dest=!destination!\%fol%_%m_d%
set op=%dest%.zip
if not exist !dest! (
echo !dest!
mkdir !dest!
) else (
:getConfirm
set _confirm=Folder Already Exists..Continue [Y/N] :
if /i "!_confirm!"=="n" (
timeout /t 3
echo "Script Execution Terminated"
exit
)
if /i not "!_confirm!"=="n" if /i not "!_confirm!"=="y" (
goto getConfirm
)
)
for %%F in (%fileList%) do (
echo Checking for the file...: %%F
REM ** Check whether the file is present in the directory --> Find command is used for handling "No files found"
for /f %%H in ('dir /s /b *%%F* 2^>nul ^| find /C /V ""') do (
REM ** If No files are found ask user confirmation
if %%H==0 (
:input
set /p confirm=File Not Found..Do you want to continue [Y/N] :
if /i "!confirm!"=="y" (
for /f %%B in ('dir /s /b *%%F*') do (
echo File Found: %%B
copy %%B "!dest!"
)
)
if /i "!confirm!"=="n" (
timeout /t 7
echo "Script Execution terminated"
exit
)
if /i not "!confirm!"=="n" if /i not "!confirm!"=="y" (
goto input
)
) else (
REM ** If files are found copy them to the destination
for /f %%B in ('dir /s /b *%%F*') do (
echo File Found: %%B
copy %%B "!dest!"
)
)
)
)
@echo off>zip.vbs
@echo 'Get command-line arguments.>>zip.vbs
@echo Set objArgs = WScript.Arguments>>zip.vbs
@echo InputFolder = objArgs(0)>>zip.vbs
@echo ZipFile = objArgs(1)>>zip.vbs
@echo REM>>zip.vbs
@echo 'Create empty ZIP file.>>zip.vbs
@echo CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" ^& Chr(5) ^& Chr(6) ^& String(18, vbNullChar)>>zip.vbs
@echo REM>>zip.vbs
@echo Set objShell = CreateObject("Shell.Application")>>zip.vbs
@echo REM>>zip.vbs
@echo Set source = objShell.NameSpace(InputFolder).Items>>zip.vbs
@echo REM>>zip.vbs
@echo objShell.NameSpace(ZipFile).CopyHere(source)>>zip.vbs
@echo REM>>zip.vbs
@echo 'Required!>>zip.vbs
@echo wScript.Sleep 2000>>zip.vbs
CScript zip.vbs !dest! .\success.zip
错误输出是:
E:\somefolder\zip.vbs(11, 1) Microsoft VBScript runtime e
rror: Object required: 'objShell.NameSpace(...)'
我的“!dest!”并且“。\ success.zip”纯粹是猜测,我将最后一段代码建在this question的第二个答案上,详细说明了如何完成。
理想情况下,我希望zip文件只在当前目录中,并且我不关心zip文件名是什么。
答案 0 :(得分:0)
以下实际上有效。
@echo off
setlocal enabledelayedexpansion
REM ** Loops through each arugment in the input file
set fileList=data
:getDestination
set /p destination=Enter the destination Folder to copy the files :
if not exist !destination! goto getDestination
for %%A in ("%~f0\..") do set "fol=%%~nxA"
set m_dd=%date%_%time%
set m_dd=%m_dd: =%
set m_dd=%m_dd::=_%
set m_dd=%m_dd:/=_%
set m_dd=%m_dd:.=_%
set m_d=%m_dd:~3,-3%
set dest=!destination!\%fol%_%m_d%
set op=%dest%.zip
if not exist !dest! (
mkdir !dest!
) else (
:getConfirm
set _confirm=Folder Already Exists..Continue [Y/N] :
if /i "!_confirm!"=="n" (
timeout /t 3
echo "Script Execution Terminated"
exit
)
if /i not "!_confirm!"=="n" if /i not "!_confirm!"=="y" (
goto getConfirm
)
)
for %%F in (%fileList%) do (
echo Checking for the file...: %%F
REM ** Check whether the file is present in the directory --> Find command is used for handling "No files found"
for /f %%H in ('dir /s /b *%%F* 2^>nul ^| find /C /V ""') do (
REM ** If No files are found ask user confirmation
if %%H==0 (
:input
set /p confirm=File Not Found..Do you want to continue [Y/N] :
if /i "!confirm!"=="y" (
for /f %%B in ('dir /s /b *%%F*') do (
echo File Found: %%B
copy %%B "!dest!"
)
)
if /i "!confirm!"=="n" (
timeout /t 7
echo "Script Execution terminated"
exit
)
if /i not "!confirm!"=="n" if /i not "!confirm!"=="y" (
goto input
)
) else (
REM ** If files are found copy them to the destination
for /f %%B in ('dir /s /b *%%F*') do (
echo File Found: %%B
copy %%B "!dest!"
)
)
)
call :zippy !dest!
)
:zippy
echo InputFolder = WScript.Arguments.Item(0)>zip.vbs
echo zipFileName = WScript.Arguments.Item(1)>>zip.vbs
echo With CreateObject("Scripting.FileSystemObject")>>zip.vbs
echo zipFile = .GetAbsolutePathName(zipFileName)>>zip.vbs
echo sourceFolder = .GetAbsolutePathName(InputFolder)>>zip.vbs
echo With .CreateTextFile(zipFile, True)>>zip.vbs
echo .Write Chr(80) ^& Chr(75) ^& Chr(5) ^& Chr(6) ^& String(18, chr(0))>>zip.vbs
echo End With>>zip.vbs
echo End With>>zip.vbs
echo With CreateObject("Shell.Application")>>zip.vbs
echo .NameSpace(zipFile).CopyHere .NameSpace(sourceFolder).Items>>zip.vbs
echo Do Until .NameSpace(zipFile).Items.Count = _>>zip.vbs
echo .NameSpace(sourceFolder).Items.Count>>zip.vbs
echo WScript.Sleep 10000>>zip.vbs
echo Loop>>zip.vbs
echo End With>>zip.vbs
CScript zip.vbs "%1" .\success.zip
答案 1 :(得分:0)
您可以使用 PowerShell Compress-Archive
,而不是创建 VBS 文件并调用它。如果它是一个 PowerShell 脚本,整个事情会更容易。如果您绝对需要将其保留为批处理脚本,那么这里是一个在末尾调用 PowerShell 的示例。
我大大简化了您的代码,同时保持了相同的行为。
@echo on
setlocal enabledelayedexpansion
:: ** Loops through each arugment in the input file
set fileList=sys-conf.txt,CNCIDNUM.txt,cnc-para.txt,pitch.txt,OPRT_HIS.txt,TOOLOFST.TXT,macro.txt,3D-INTER.txt,ATCMagazine_,Makino_AtdAxis_CMN_,Makino_AtdAxis_Duty_,Makino_Atdspindle_,Makino_IOlink_setting,Makino_MMS_,Makino_Parameter_,Makino_QAC_CurveFit_,Makino_QAC_Override_,Makino_QAC_Setting_,PalletMagazine_,MCA_selectData.txt
:: You can pass the directory as an argument
if [%1]==[] (
:getDestination
set /p destination=Enter the destination Folder to copy the files :
if not exist !destination! goto getDestination
) else (
set destination=%1
)
for %%A in (.) do set "fol=%%~nxA"
set m_dd=%date%_%time%
set m_dd=%m_dd: =%
set m_dd=%m_dd::=_%
set m_dd=%m_dd:/=_%
set m_dd=%m_dd:.=_%
set m_d=%m_dd:~3,-3%
set dest=%destination%\%fol%_%m_d%
set op=%dest%.zip
echo Testing if destination exists: %dest%
if not exist %dest% (
mkdir "%dest%"
) else (
:getConfirm
set _confirm=Continue? [Y/N] :
if /i "!_confirm!"=="n" (
timeout /t 3
echo Script Execution Terminated
exit
)
if /i not "!_confirm!"=="y" (
echo Invalid response. Enter only 'y' or 'n'
goto getConfirm
)
)
for %%F in (%fileList%) do (
copy "%%F" "%dest%"
)
echo Running PowerShell to compress: %dest%
powershell "Compress-Archive -Path """%dest%\*""" -DestinationPath """%op%""""