CMD Syntax for local command - cmd

I would like to run a script, copy my folder and start rundll32 in my copied folder.
mkdir "c:\SmartCard_Treiber"
xcopy "%~DP0SmartCard_Treiber\*" "c:\SmartCard_Treiber" /i /s
cmd /c cd "c:\SmartCard_Treiber" & RUNDLL32.EXE AdvPack.dll,LaunchINFSection Gemalto.inf,,0
TIMEOUT /T 30 /NOBREAK
rd "C:\SmartCard_Treiber" /Q /S

mkdir "c:\SmartCard_Treiber"
xcopy "%~DP0SmartCard_Treiber\*" "c:\SmartCard_Treiber" /i /s
pushd "c:\SmartCard_Treiber"
RUNDLL32.EXE AdvPack.dll,LaunchINFSection Gemalto.inf,,0
TIMEOUT /T 30 /NOBREAK
popd
rd "C:\SmartCard_Treiber" /Q /S
Change to the needed directory (pushd) and when finished return to the previous one (popd)

Related

How to delete subfolders and files from parent folders found in the variable of a bat script?

What I'm trying to do is find all the folders named "temp" on local disk C and then delete all files and subfolders that are inside it, but I don't know how to delete it, I only managed to get to the part of finding all folders and put them in the variable, but I don't know the correct code to delete. Help.
#echo off
setlocal
setlocal enabledelayedexpansion
#echo off
for /d /r "c:\" %%i in (temp) do (
#if exist "%%i" (
#set _variable=%%i
#echo !_variable!
)
)
endlocal
#echo off && setlocal enabledelayedexpansion
cd /d "C:\" && for /f tokens^=* %%i in ('dir /s /b /a:d "temp"')do (
echo\ Current Folder: "%%~i"
echo\ RmDir /q /s "%%~i"
echo\ MkDir "%%~i"
)
%__APPDIR__%\Timeout.exe /t -1 & endlocal & goto :EOF
1) Go to the drive:
cd /d "C:\"
2) Use For /f with dir /s /b /a:d instead For /D /R
for /f tokens^=* %%i in ('dir /s /b /a:d "temp"')do...
3) Don't need to delete the files in the target folder, just remove this folder and recreate it again...
RmDir /q /s "%%~i" && MkDir "%%~i\*"
Outputs look like this:
Current Folder: "C:\Program Files (x86)\Google\Temp"
rmdir /q /s "C:\Program Files (x86)\Google\Temp"
mkdir "C:\Program Files (x86)\Google\Temp\"
Current Folder: "C:\Program Files (x86)\Steam\steamapps\temp"
rmdir /q /s "C:\Program Files (x86)\Steam\steamapps\temp"
mkdir "C:\Program Files (x86)\Steam\steamapps\temp\"
Current Folder: "C:\ProgramData\Microsoft\Windows\WER\Temp"
rmdir /q /s "C:\ProgramData\Microsoft\Windows\WER\Temp"
mkdir "C:\ProgramData\Microsoft\Windows\WER\Temp\"
Current Folder: "C:\Users\All Users\Microsoft\Windows\WER\Temp"
rmdir /q /s "C:\Users\All Users\Microsoft\Windows\WER\Temp"
mkdir "C:\Users\All Users\Microsoft\Windows\WER\Temp\"
Current Folder: "C:\Users\Default\AppData\Local\Temp"
rmdir /q /s "C:\Users\Default\AppData\Local\Temp"
mkdir "C:\Users\Default\AppData\Local\Temp\"
Current Folder: "C:\Users\ecker\AppData\Local\Temp"
rmdir /q /s "C:\Users\ecker\AppData\Local\Temp"
mkdir "C:\Users\ecker\AppData\Local\Temp\"
Current Folder: "C:\Users\ecker\AppData\Local\Intel\CUIPromotions\Temp"
rmdir /q /s "C:\Users\ecker\AppData\Local\Intel\CUIPromotions\Temp"
mkdir "C:\Users\ecker\AppData\Local\Intel\CUIPromotions\Temp\"
Current Folder: "C:\Users\ecker\AppData\Local\Packages\windows_ie_ac_001\AC\Temp"
rmdir /q /s "C:\Users\ecker\AppData\Local\Packages\windows_ie_ac_001\AC\Temp"
mkdir "C:\Users\ecker\AppData\Local\Packages\windows_ie_ac_001\AC\Temp\"
Current Folder: "C:\Windows\Temp"
rmdir /q /s "C:\Windows\Temp"
mkdir "C:\Windows\Temp\"
Current Folder: "C:\Windows\assembly\temp"
rmdir /q /s "C:\Windows\assembly\temp"
mkdir "C:\Windows\assembly\temp\"
mkdir "C:\Windows\assembly\NativeImages_v4.0.30319_64\Temp\"
Current Folder: "C:\Windows\System32\DriverStore\Temp"
rmdir /q /s "C:\Windows\System32\DriverStore\Temp"
mkdir "C:\Windows\System32\DriverStore\Temp\"
Current Folder: "C:\Windows\WinSxS\Temp"
rmdir /q /s "C:\Windows\WinSxS\Temp"
mkdir "C:\Windows\WinSxS\Temp\"
Verify in your test the outputs and for effective execution, remove echo in:
#echo off && setlocal enabledelayedexpansion
cd /d "C:\" && for /f tokens^=* %%i in ('dir /s /b /a:d "temp"')do (
echo\ Current Folder: "%%~i"
echo\ RmDir /q /s "%%~i"
echo\ MkDir "%%~i"
)
%__APPDIR__%\Timeout.exe /t -1 & endlocal & goto :EOF
Or use this short option to suppress a possible error message for any folder in use or some other type of error
#echo off
setlocal enabledelayedexpansion && cd /d "C:\"
for /f "tokens=*" %%i in ('dir /s /b /a:d "temp"
')do 2>nul >nul (RmDir /q /s "%%~i" && MkDir "%%~i")
"%__APPDIR__%\Timout.exe" -1 & endlocal & goto :EOF
And, of course, a slightly smaller option...
#echo off
cd/d "C:\" & setlocal enabledelayedexpansion
for /f tokens^=* %%i in ('dir/s/b/a:d "temp"
')do 2>nul (RmDir /q /s "%%~i" && MD "%%~i")
"%__APPDIR__%\Timeout.exe" /t -1 && endlocal
For command line help, you can use /?:
RmDir /?, For /?, MkDir /?, Endlocal /?, SetLocal /?
On the internet, you can get more help on:
For /f
Endlocal
Goto :EOF
Timeout.exe
EnableDelayedExpansion
Conditional Execution || && ...

Deleting old files in Windows 10 using forfiles

I am trying to delete old files and folders in my Google Drive using a bat script on Windows 10 Home.
forfiles /M *.* /P "C:\Users\bruker\Google Drive\" /S /D -180 /C "cmd /c del /F /Q #path"
Gives the error message:
ERROR: Invalid argument/option - '/F'. Type "FORFILES /?" for usage.
Anyone have a solution for this?
The quotes aren't needed. Both forfiles ... /C cmd /c del /F /Q #path and forfiles ... /C (cmd /c del /F /Q #path) should work.

Simplifying batch backup script

I've made this script (minus all my ECHO for your readability) to backup certain user folders to an external device. It's working flawlessly, but I'm wondering if anyone has any ideas as to how I could simplify it (eg. more 'clever').
I'm new to this site and coding. Please bear with me!
All help appreciated.
#ECHO OFF
SET driveLetter=%~d0
:CHOOSE
SET /P CHOOSE=Are you sure you want to continue [Y/N]?
IF /I "%CHOOSE%" == "Y" GOTO :chooseYes
IF /I "%CHOOSE%" == "N" GOTO :chooseNo
GOTO :CHOOSE
:chooseYes
MKDIR %driveLetter%\Desktop
MKDIR %driveLetter%\Documents
MKDIR %driveLetter%\Favorites
MKDIR %driveLetter%\Pictures
MKDIR %driveLetter%\Downloads
TIMEOUT /T 1 /NOBREAK >NUL
ROBOCOPY %USERPROFILE%\Desktop\ %driveLetter%\Desktop /E /COPYALL /ZB /MT:20 /XJ /R:2 /W:5
ROBOCOPY %USERPROFILE%\Documents\ %driveLetter%\Documents /E /COPYALL /ZB /MT:20 /XJ /R:2 /W:5
ROBOCOPY %USERPROFILE%\Favorites\ %driveLetter%\Favorites /E /COPYALL /ZB /MT:20 /XJ /R:2 /W:5
ROBOCOPY %USERPROFILE%\Pictures\ %driveLetter%\Pictures /E /COPYALL /ZB /MT:20 /XJ /R:2 /W:5
ROBOCOPY %USERPROFILE%\Downloads\ %driveLetter%\Downloads /E /COPYALL /ZB /MT:20 /XJ /R:2 /W:5
CLEANMGR /C: /SAGERUN:65535 /SETUP
TIMEOUT /T 1 /NOBREAK >NUL
DEFRAG /C /H /V /W
PAUSE
EXIT
:chooseNo
TIMEOUT /T 3 /NOBREAK >NUL
Best regards.
If possible, don't repeat yourself
:chooseYes
for %%a in ( Desktop Documents Favorites Pictured Downloads ) do (
robocopy "%userprofile%\%%a" "%driveLetter%\%%a" /E /COPYALL /ZB /MT:20 /XJ /R:2 /W:5
)
CLEANMGR /C: /SAGERUN:65535 /SETUP
TIMEOUT /T 1 /NOBREAK >NUL
DEFRAG /C /H /V /W
PAUSE
EXIT
note: the mkdir has been supressed as the robocopy command will create the target folder

How to use multiple commands in batch using forfiles command?

#ECHO OFF
SET backdir=backup
SET snapshotdir=snapshots
SET worldprefix=world_
SET itdate=%date:~10,4%-%date:~4,2%-%date:~7,2%
SET hour=%time:~0,2%
IF "%hour:~0,1%" == " " SET hour=0%hour:~1,1%
echo Current date: %itdate%. Current hour: %hour%. Current Minute:Second: %time:~3,2%:%time:~6,2%
forfiles /m "%worldprefix%*" /c (
echo Copying World: #path
cmd /c xcopy /e /c /h /i /v /r /y /q #file %snapshotdir%\#file\%itdate%-%hour%-%time:~3,2%-%time:~6,2%
cmd /c xcopy /e /c /h /i /v /r /y /q #file %backdir%\%itdate%D\worlds\#file
)
echo Copying Plugins
xcopy /e /c /h /i /v /r /y /q plugins %backdir%\%itdate%D\plugins\
xcopy /e /c /h /i /v /r /y /q %backdir%\%itdate%D %backdir%\%itdate%-%hour%H\
echo Backup Complete (assuming no errors above). Attempting to remove old files..
forfiles /p "%snapshotdir%" /c "cmd /c rmdir /s /q #path" /d -7
forfiles /p "%backdir%" /m "*H" /c "cmd /c rmdir /s /q #path" /d -2
forfiles /p "%backdir%" /m "*D" /c "cmd /c rmdir /s /q #path" /d -14
PAUSE
I am trying to copy all files with "world_" as a prefix. I run into a problem when I try to use multiple commands in a loop. I have attempted to write the batch script I want above.
The two commands have absolutely nothing in common, so no, you cannot use parentheses like that.
You must execute all the commands within a single CMD /C. You can concatenate commands on one line using &. I've defined a simple XCOPY "macro" to save a bit of typing.
set XCOPY=xcopy /e /c /h /i /v /r /y /q"
forfiles /m "%worldprefix%*" /c "cmd /c echo Copying World: #path&%XCOPY% #file %snapshotdir%\#file\%itdate%-%hour%-%time:~3,2%-%time:~6,2%&%XCOPY% #file %backdir%\%itdate%D\worlds\#file"
If you escape the quotes, then you can use line continuation to split the logical line accross multiple lines. But then you must also escape the &.
set XCOPY=xcopy /e /c /h /i /v /r /y /q"
forfiles /m "%worldprefix%*" /c ^"cmd /c ^
echo Copying World: #path ^&^
%XCOPY% #file %snapshotdir%\#file\%itdate%-%hour%-%time:~3,2%-%time:~6,2% ^&^
%XCOPY% #file %backdir%\%itdate%D\worlds\#file^"
Or you could put the & in the front of the line so that you don't need to escape it. The line continuation also escapes the first character of the next line:
set XCOPY=xcopy /e /c /h /i /v /r /y /q"
forfiles /m "%worldprefix%*" /c ^"cmd /c ^
echo Copying World: #path^
&%XCOPY% #file %snapshotdir%\#file\%itdate%-%hour%-%time:~3,2%-%time:~6,2%^
&%XCOPY% #file %backdir%\%itdate%D\worlds\#file^"
I realize this is an old post, but another way of doing it is to call a batch file and pass it the parameters that it needs. And I do not believe that there is any limitations in what can be in that batch file. For example:
forfiles /M "*.img" /C "cmd /c ProcessFile.bat #file #fdate #ftime"

Batch: Copying from computer to network drive

I've written a batch file that will do some tests then copy the results to a network drive. This use to work fine for me but over the last few days I have noticed it doesn't work for me and other people have said it has never worked for them.
This is the section that I am calling to copy to M: which all computers on our network can see. The section is being called and the archived results folder is being created but nothing is being sent to the destination drive.
Can anybody see what I've done wrong?
:CopyResults
SET CLASSPATH=%CLASSPATH_ORIGINAL%
For /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set mydate=%%a-%%b-%%c)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
set source="target"
set Archive= "Archived Results\Test Results - %CLIENTCHOICE% %mydate% At %mytime% Using %BROWSEROPTION%"
set ScreenshotDestination= "Archived Results\Test Results - %CLIENTCHOICE% %mydate% At %mytime% Using %BROWSEROPTION%\Screenshots"
mkdir %Archive%
echo %Archive%
echo d | xcopy %source% %Archive% /k /e /d /y /h /i
rem copy screenshots
set screenshots="Screenshots"
echo d | xcopy %screenshots% %ScreenshotDestination% /k /e /d /y /h /i
%Archive%\site\index.html
rem copy accross to M:
set Destination= "M:\ProductTesting\AutomationResults\%CLIENTCHOICE%\%mydate% At %mytime% Using %BROWSEROPTION% Ran By %username%"
set ScreenshotDestination= "M:\ProductTesting\AutomationResults\%CLIENTCHOICE%\%mydate% At %mytime% Using %BROWSEROPTION%\Screenshots"
set source="target"
mkdir %Destination%
echo %Destination%
echo d | xcopy %source% %Destination% /k /e /d /y /h /i
rem copy screenshots
set screenshots="Screenshots"
echo d | xcopy %screenshots% %ScreenshotDestination% /k /e /d /y /h /i
GOTO Del

Resources