xcopy not copying directories. unsure why - 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).

Related

Called Network Batch with Constant "Path not Found"

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

Batch - Move all directories in directory to another

I just try move all directories from directory "D:\Download" to "D:\DownloadArchive"
I try this but doesnt work
move /y "D:\Download\*.*" "D:\DownloadArchive"
Use "FOR /D" to select only directories. (Disclaimer: untested)
FOR /d %1 IN ("D:\Download\*") DO move /y "%1" "D:\DownloadArchive"
A very simple solution is to use a FOR statement to move each file in the directory. A better alternative to move is xcopy as it allows to over-wright existing files.
For the switches I'm using type xcopy /? in an CMD Window.
From Batch:
FOR /d %%i IN ("D:\Download\*") DO xcopy "%%i" "D:\DownloadArchive" /E /I /Y /S
From CMD:
FOR /d %i IN ("D:\Download\*") DO xcopy "%i" "D:\DownloadArchive" /E /I /Y /S

check the existence of the folder and delete the temp files

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.

Copying a folder into multiple folders

I have a folder with some content which I want to copy into 260 folders, which all are in the same folder.
I guess this is doable with a batch file and a for-loop, but I can't seem to understand how to do it.
you can use XCopy for copy the file
xcopy "src\*.*" "..\..\..\dst" /s /i /c /y
and can add the loop FOR loop
for /l %%a in (1,1,260) do (
call :Method1
)
:Method1
xcopy "src\*.*" "..\..\..\dst" /s /i /c /y
GOTO :EOF
Update
FOR /D %%a in ("dst\*") do
xcopy "src\*.*" "%%a" /s /i /c /y

Bat file - remove and copy

I want to remove all directory content (subdirectories and files, but not the main directory).
After that I want to copy all content from other directory to that directory.
How could I do this?
This code doesn't work
cd C:\directory1
rmdir /s/q
pause
xcopy C:\directory2\directory22 C:\directory1 /s /e
rmdir needs a directory to delete, so this will work, but you will get an error message:
cd C:\directory1
rmdir . /s /q
pause
xcopy C:\directory2\directory22 C:\directory1 /s /e
But you can also do this:
cd C:\
rmdir C:\directory1 /s /q
md C:\directory1
pause
xcopy C:\directory2\directory22 C:\directory1 /s /e

Resources