i want to make an auto backup script for my office. i've do some research and finally get some light bulb, but the problem is that my code just work in Windows 7 and won't work in Windows XP. so how do i solve this? Here is my code
echo off
setlocal enabledelayedexpansion
:FIND.BACKUP.DRIVE.LETTER
set start.dir=%systemdrive%
J:
%cls%
if not "%cd:~0,2%"=="%start.dir%" goto next
I:
%cls%
if not "%cd:~0,2%"=="%start.dir%" goto next
H:
%cls%
if not "%cd:~0,2%"=="%start.dir%" goto next
G:
%cls%
if not "%cd:~0,2%"=="%start.dir%" goto next
if "%cd:~0,2%"=="%start.dir%" (
echo WAITING FOR FLASH MEDIUM TO BE CONNECTED . . .
if exist "%windir%\system32\timeout.exe" timeout /t 1 /nobreak >nul
if not exist "%windir%\system32\timeout.exe" pause
goto FIND.FLASH.DRIVE.LETTER
)
:next
set start.hour=%time:~0,2%
set start.min=%time:~3,2%
REM FLASH.DRIVE IS THE FLASH DRIVE.
set flash.drive=%cd:~0,2%
set date="%date:~7,2%-%date:~4,2%-%date:~10,4%"
mkdir "%userprofile%\desktop\a\%date%"
xcopy "%flash.drive%\*.*" /s /c /d /e /h /i /r /y %userprofile%\desktop\a\%date%
if not exist %flash.drive% set /a total.time=((((%time:~0,2%-%start.hour%)... && echo Flash Drive has been in for !total.time! minutes. && Pause>nul
#pause
thanks
This should be reliable, to do the same task.
Are you sure that you need the /d switch in xcopy?
#echo off
:loop
echo waiting... Please insert the flash drive...
ping -n 3 "" >nul
set "drv="
for %%a in (g h i j) do if exist "%%a:\" set "drv=%%a:"
if not defined drv goto :loop
REM drv IS THE FLASH DRIVE.
set start.hour=%time:~0,2%
set start.min=%time:~3,2%
set "d8=%date:~7,2%-%date:~4,2%-%date:~10,4%"
xcopy "%drv%\*.*" /s /c /d /e /h /r /y "%userprofile%\desktop\a\%d8%\"
set end.hour=%time:~0,2%
set end.min=%time:~3,2%
pause
I believe your calculation of total time is broken, and is something that you can work on.
Related
#echo on
set choice=
:D
if exist D: (set choice=D:) if exist (choice=D:) goto copyG else goto E
:E
if exist E: (set choice=E:) if exist (choice=E:) goto copyG else goto F
:F
if exist F: (set choice=F:) if exist (choice=F:) goto copyG else goto H
:H
if exist H: (set choice=H:) if exist (choice=H:) goto copyG
:copyG
xcopy /e /y G:\2019-Year10\*.* "%choice%"
pause
:removed
echo removed
This is my current code.
But is does not copy the files to the USB from the G drive.
Is there a way to fix this?
Edit:
Thanks got it working in the end i used this
#echo off
echo.
echo Finding Drive to copy work from "G:\2019-Year10"
echo.
for %%i in (D: E: F: H:) do (
if exist %%i set drive=%%i
)
xcopy /e /y "G:\2019-Year10\*.*" "%drive%\2019-Year10\"
echo.
echo Files coped to "%drive%"
echo.
echo.
echo Remove %drive% Drive
:removle_of_drive
if exist %drive% (goto removle_of_drive) else exit
It knows when you take out the USB and closes the batch file, and gives you more info on what it is doing
thanks for the help: Gerhard Barnard
So here we can use wmic to determine the disk type. Note!!! If you have more than one USB attached, this will not do as intended:
#echo off
for /F "tokens=1-4" %%a in ('wmic logicaldisk get caption^,description^,drivetype 2^>nul') do (
if %%l equ 2 (
echo xcopy /e /y "G:\2019-Year10\*.*" "%%d"
)
)
pause
Also Note I added echo to the xcopy line to simulate the command, only remove echo to perform the ACTUAL xcopy command once you are happy that it will do what you want it to.
But to basically show you how the choice one should work (as per your example):
CHOICE /C DEFH /M "Choose driveletter "
if errorlevel 4 set "Drive=H:"
if errorlevel 3 set "Drive=F:"
if errorlevel 2 set "Drive=E:"
if errorlevel 1 set "Drive=D:"
echo xcopy /e /y "G:\2019-Year10\*.*" "%Drive%\"
Or to automate it based on your know drive letters, instead of detecting it like the first example (as per your example)
#echo off
for %%i in (D: E: F: H:) do (
if exist %%i echo xcopy /e /y "G:\2019-Year10\*.*" "%%i\"
)
My batch script can not be run from network source, that's why it's gonna make a copy of itself to the desktop and starts the new copy, on finish it's deleting the copy.
:: Check location
if "%~dp0" == "%userprofile%\Desktop\" goto:eof
xcopy /I /Y "%~dpnx0" "%userprofile%\Desktop\" >nul 2>&1
start "new window" cmd /c %userprofile%\Desktop\batchname.bat
exit
That works fine, my issue is, %cd% or %0 is not updated and is still acting like the original script. Shows the location of the original script.
How can I check the location, make a self-copy to the desktop and start it, like it was double-clicked within windows? Because the script only fails, if it's started through the original script of the share.
What happens:
Starting script XYZ.bat from network share
Script notices not being located on desktop
Script does self-copy to desktop
Script runs copy from desktop
Script ends
Copy launched from original script
Script located on desktop >> fine
Script reads infos of script header:
for /F "tokens=3-8 delims= " %%a in ('findstr /B /C:":: Drive:" "%~dpnx0"') do (
Script fails because %~dpnx0 contains path of original script which is unavailable because all network shares are removed at this point
Any suggestions?
PS: I'm new here, I hope my English is understandable. Cheers
EDIT:
Thanks for the help, the thing with the path is fixed now and the script works fine, as long as I don't turn on the "remove all existing drives" function. If I do so the following happens:
Previous steps taken:
Script checks location and copy itself to the users desktop and starts from there, also all drives are removed, than it fails on the findstr thingy with the error:
[Drive-Mapper:] All map drives removed
[Drive-Mapper:] Mapping drives now:
Das aktuelle Verzeichnis ist ungültig. The current
folder does not exist
[Drive-Mapper:] Successfully finished << I wish..
Drücken Sie eine beliebige Taste . . . Press any key to continue
The Mapping function:
:mapdrives
%say% Mapping drives now:
set errorcount=0
for /F "tokens=3-8 delims= " %%a in ('findstr /B /L /C:":: Drive:" "%~f0"') do (
REM echo Server=%%a User=%%b Letter=%%c drive=%%d nick=%%e
REM if "%%b" == "all" OR if "%%b" == "%username%" (
if "%%b" == "all" (
>nul 2>&1 net use %%c: \\%%a\%%d /persistent:yes
if errorlevel 1 (%say2% Failed %%c: \\%%a\%%d & set /a errorcount=errorcount+1) else (
if "%%e" == "" (
:: Rename without nick
>nul 2>&1 reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##%%a#%%d /v _LabelFromReg /d "%%d (%%a)" /f
%say2% Successfully %%c: \\%%a\%%d
) else (
:: Rename with nick
>nul 2>&1 reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##%%a#%%d /v _LabelFromReg /d "%%e (%%a)" /f
%say2% Successfully %%c: \\%%a\%%d # %%e
)
)
)
if "%%b" == "%username%" (
>nul 2>&1 net use %%c: \\%%a\%%d /persistent:yes
if errorlevel 1 (%say2% Failed %%c: \\%%a\%%d & set /a errorcount=errorcount+1) else (
if "%%e" == "" (
:: Rename without nick
>nul 2>&1 reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##%%a#%%d /v _LabelFromReg /d "%%d (%%a)" /f
%say2% Successfully %%c: \\%%a\%%d
) else (
:: Rename with nick
>nul 2>&1 reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##%%a#%%d /v _LabelFromReg /d "%%e (%%a)" /f
%say2% Successfully %%c: \\%%a\%%d # %%e
)
)
)
%sf_wait2%
)
%sf_wait%
if "%errorcount%" == "0" (%say% Successfully finished) else (%say% Warning %errorcount% Errors!!)
%say2% & pause
goto:eof
Script crabs infos from batch header:
:: - Force - Deleting drives (0=No,1=Yes)
set force_del_drives=1
:: - Force - Kill Explorer (0=No,1=Yes)
set force_kill_explorer=1
::----------------------------------------------------------------------------------------
:: HINT SERVER USER LETER DRIVE NICKNAME (IF NOT USING DRIVENAME)
:: Drive: server all H Home My Home
:: Drive: server all V Drive1
:: Drive: server all M Drive2
::----------------------------------------------------------------------------------------
:: -Drive: server user I DisabledDrive
:: Drive: server user K Drive4
:: Drive: server user Z Homes All Homes
PS: If I run the script from c:\ it will copy itself to desktop and runs perfectly, it also works like I said, if I dont remove the network drive the script originaly was executed from.
Any ideas?
#echo off
setlocal
:: Check location
if not exist "%userprofile%\Desktop\" (
>&2 echo Desktop folder not exist
exit /b 1
)
if "%~dp0" == "%userprofile%\Desktop\" goto :main
xcopy /I /Y "%~f0" "%userprofile%\Desktop\" >nul 2>&1
start "new window" "cmd /c "%userprofile%\Desktop\%~nx0""
goto :eof
:main
echo %~f0
pause
goto :eof
Added check for Desktop folder as it is a Shell Folder and may
not exist in that location.
Logic changed to go to :main label if location is Desktop folder.
Command of start modified to handle the script name and extension
without hard coding a name and extension.
As part of my job I need to remove files from computers that are being shipped over seas, this task is done several times a week and can be scripted, however I am terrible with script, I am in no means a coder of any sort.
My issue is that I need to clear out the C:\Users folder whilst keeping the Public and Default folders. I know how to remove a directory and all subfolders however I don't know how to keep just those two folders.
The code I have so far is:
#Echo off
color F
:Choice
Echo.
Echo.
set choice=
set /p choice="Type Y to proceed or N to close the tool and then press ENTER:----"
IF '%Choice%' =='Y' GOTO DELETE
IF '%Choice%' =='y' GOTO DELETE
IF '%Choice%' =='N' GOTO END
IF '%Choice%' =='n' GOTO END
:DELETE
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
Echo.
Echo.
#Echo All Files Removed
GOTO END
:END
echo.
echo.
Echo *****...Closing programme... Please wait...*****
Timeout /t 3
exit
Do you know if this is possible?
The following method, ran As Administrator, may work for you:
#Echo Off
Color 0F
Echo=&Echo=
Choice /N /M "Type Y to proceed or N to close the tool"
If ErrorLevel 2 Exit /B
For /F Tokens^=2^Delims^=^" %%A In ('WMIC Path Win32_UserProfile Where^
"Special!='True'" Assoc /AssocClass:Win32_UserAccount 2^>Nul'
) Do WMIC UserAccount Where "SID='%%A' And LocalAccount='TRUE'" Delete
Echo=&Echo=&Echo All Files Removed&Echo=&Echo=
Echo *****...Closing programme... Please wait...*****
Timeout 3 /NoBreak>Nul
Exit /B
Special!='True' will ignore special accounts, which include Administrator, Public and Default.
I have a folder with 110.000 files and I want a way to break this folder into multiple subfolders containing say 3000 files each (with a batch script perhaps?). (Trying a copy/paste with WinExplorer gets stuck in "Preparing to Copy".)
For example:
BigFolder
|
NewFolder
| | | | |
Sub1 Sub2 Sub3 Sub4 Sub5...
I am surprised to find the same case of mine.
I had 30,000 files that needed to be sorted, so I asked question on this page:
Fast methods to copy(move) files in batch file
This is Compo's script:
#Echo Off
If /I Not "%__CD__%"=="%~dp0" PushD "%~dp0" 2>Nul||Exit/B
SetLocal EnableDelayedExpansion
Set "DirN=-1"
:Check_DirN
Set/A "DirN+=1"
If Exist "%DirN%" GoTo Check_DirN
Set "limit=700"
For %%A In (*.bat *.cmd *.txt) Do (
If Not Exist "%DirN%" MD "%DirN%"
If /I Not "%%~nxA"=="%~nx0" RoboCopy . "%DirN%" "%%A" /MOV 1>NUL
Set/A "limit-=1"
If !limit! Lss 0 GoTo Check_DirN
)
Echo(Task Done!
Timeout -1 1>Nul
And this is what I use and I edited for a bit for the purpose:
#Echo Off
If /I Not "%__CD__%"=="%~dp0" PushD "%~dp0" 2>Nul||Exit/B
taskkill /f /im explorer.exe >nul
taskkill /f /im SearchIndexer.exe >nul
sc stop WSearch >nul
sc config WSearch start= disabled >nul
SetLocal EnableDelayedExpansion
Set "DirN=-1"
:Check_DirN
Set/A "DirN+=1"
If Exist "%DirN%" GoTo Check_DirN
cls
echo Moving files to Directory %DirN%...
Set "limit=2999"
MD "%DirN%"
For %%A In (*.html) Do (
RoboCopy . "%DirN%" "%%A" /MOV 1>NUL
Set/A "limit-=1"
If !limit! Lss 0 GoTo Check_DirN
)
Echo(Task Done!
start explorer.exe
start SearchIndexer.exe
sc config WSearch start= delayed-auto >nul
sc start WSearch >nul
Timeout -1 1>Nul
You can remove taskkill, start and sc part if desired.
I added this part because explorer and Windows Search Indexer will cause waste of memory when moving files. I recommend you to run the script with Administrator privilege.
Try to test the script in small scale to see if it does work.
I'm trying to take a list of user numbers in a text file...;
e.g.
002
003
004
.
.
.
408
409
etc
...create an IP address based on this number, make a connection to the IP address then copy a folder from this location to a network location;
REM *******************************************************************
REM Get User number from Text File and remove leading zero's if present
REM *******************************************************************
FOR /F "tokens=* delims=0" %%A IN (C:\Users.txt) DO SET host=%%A
pause
REM **************************************
REM Calculate IP Address from User number
REM **************************************
If %host% gtr 250 goto :continue
set userip=18.100.%host%.100
goto :IPAttained
:continue
If %host% gtr 500 goto :continue2
set /a host=%host% - 250
set userip=18.101.%host%.100
goto :IPAttained
:continue2
set /a host=%host% - 500
set userip=18.102.%host%.100
:IPAttained
pause
net use N: \\%userip%\d$ /user:%%Acom\master password
xcopy "\\%userip%\d$\Time Sheet" "\\NetworkLocation\Users\%%Acom\Time Sheet" /E /C /R /I /K /Y
net use N: /delete
)
So far it appears to work...however it runs through the entire list and then works out the IP address on the last line in the text file!!
I'd like it to run for every line of course but I'm unsure what I'm missing here. Hope that makes sense.
#ECHO OFF
SETLOCAL
FOR /f %%a IN (q24533225.txt) DO (
CALL :copyts %%a
)
GOTO :EOF
:copyts
SET host=%1
:copytsl
IF DEFINED host IF %host:~0,1%==0 SET host=%host:~1%&GOTO copytsl
SET /a nic=100
:SEThostl
IF %host% gtr 250 set /a nic+=1&SET /a host-=250&GOTO sethostl
SET "userip=18.%nic%.%host%.100"
ECHO(net use N: \\%userip%\d$ /user:%1com\master password
ECHO(xcopy "\\%userip%\d$\Time Sheet" "\\NetworkLocation\Users\%1com\Time Sheet" /E /C /R /I /K /Y
ECHO(net use N: /delete
GOTO :eof
It would appear that you want 18.100.1-250.100, 18.101.1-250.100, 18.102.1-250.100 etc.
The above routine will simply ECHO the required commands. After testing and verification, remove the three ECHO( to activate the commands.
I used a file named q24533225.txt containing your data for my testing. The filename is unimportant, of course - but be very careful testing any solution against 008, 009 and 249..251 where the calculations are taking place. You don't unfortunately specify what the results in these cases should be and we're forced to derive the formula from your code and make assumptions.
your problem is that your for loop spins through the file, and does nothing but set a variable, so it resets it every iteration, effectively storing the value on the last line by the end of the loop. i cant test this, but here is some example code of one way you might fix the problem.
set line=0
set ohost=000
:top
REM *******************************************************************
REM Get User number from Text File and remove leading zero's if present
REM *******************************************************************
FOR /F "skip=%line% tokens=* delims=0" %%A IN (C:\Users.txt) DO #( SET host=%%A&goto break)
:break
set /a line=line+1
if %host% EQU %ohost% (
goto :EOF
) else (
set ohost=%host%
)
REM **************************************
REM Calculate IP Address from User number
REM **************************************
If %host% gtr 250 goto :continue
set userip=18.100.%host%.100
goto :IPAttained
:continue
If %host% gtr 500 goto :continue2
set /a host=%host% - 250
set userip=18.101.%host%.100
goto :IPAttained
:continue2
set /a host=%host% - 500
set userip=18.102.%host%.100
:IPAttained
pause
net use N: \\%userip%\d$ /user:%%Acom\master password
xcopy "\\%userip%\d$\Time Sheet" "\\NetworkLocation\Users\%%Acom\Time Sheet" /E /C /R /I /K /Y
net use N: /delete
)
goto top
The main problem with your code is that all the address logic is out of the for do clause, so, the input file is readed, assigning the value of each of the lines to the variable, and when the last line is readed, for command finishes its work and the batch file continues.
An alternative is to move all the address management to a subroutine that gets called for each value readed from the input file, passing the readed value as a parameter
FOR /F "delims=" %%A IN (C:\Users.txt) DO call :process %%A
pause
exit /b
:process
rem read passed parameter
set "host=%1"
If %host% gtr 250 goto :continue
set /a host=1%host%-1000
set userip=18.100.%host%.100
goto :IPAttained
:continue
If %host% gtr 500 goto :continue2
set /a host=%host% - 250
set userip=18.101.%host%.100
goto :IPAttained
:continue2
set /a host=%host% - 500
set userip=18.102.%host%.100
:IPAttained
pause
net use N: \\%userip%\d$ /user:%1com\master password
xcopy "\\%userip%\d$\Time Sheet" "\\NetworkLocation\Users\%1com\Time Sheet" /E /C /R /I /K /Y
net use N: /delete
goto :eof
Or you can put your code INSIDE the do clause (parenthesis enclosed), but have to reformat how this work, as any goto command executed inside a for block automatically finishes the processing of the for. It is more clear and efficient, but "requires" delayed expansion for it to work
#echo off
setlocal enableextensions enabledelayedexpansion
for /f "delims=" %%a IN (C:\Users.txt) DO (
if %%a gtr 500 (
set /a "host=%%a-500"
set "userip=18.102.!host!.100"
) else if %%a gtr 250 (
set /a "host=%%a-250"
set "userip=18.101.!host!.100"
) else (
set /a "host=1%%a-1000"
set "userip=18.100.!host!.100"
)
net use N: \\!userip!\d$ /user:%%acom\master password
xcopy "\\!userip!\d$\Time Sheet" "\\NetworkLocation\Users\%%acom\Time Sheet" /E /C /R /I /K /Y
net use N: /delete
)
endlocal
pause