Playing around with my Batch script again. It was brought to my attention that my update routine was not working correctly. Iteration 1 would result in the running script being deleted and not copying the new version from a folder I had selected. Iteration 2 now deletes the old script and copies the new script but with each line of code I change I end up with the currently running script closing, and the new updated script not launching.
Here's a snippet of the Update routine:
:Options
REM I'll set the variable here to what is set elsewhere in the script.
SET VERSION=Version 1.7
CLS
ECHO.
ECHO.
ECHO %VERSION%
ECHO.
ECHO.
ECHO ==================== OPTIONS =====================
ECHO.
ECHO 1. Update Script WIP
ECHO 2. Install entire script to Statup Folder
ECHO 3. Install individual items for auto-launch
ECHO 4. Install Windows Logout feature
ECHO 5. Uninstall
ECHO 6. Change Colors
ECHO 7. Changelog
ECHO.
ECHO =======PRESS 'Q' TO QUIT OR 'B' TO GO BACK========
ECHO.
SET OPTION=
SET /P OPTION=Select an Option:
IF /I '%OPTION%'=='1' GOTO Update
IF /I '%OPTION%'=='2' GOTO Install
IF /I '%OPTION%'=='3' GOTO I_Install
IF /I '%OPTION%'=='4' GOTO I_Logout
IF /I '%OPTION%'=='5' GOTO Uinstall
IF /I '%OPTION%'=='6' GOTO Colors
IF /I '%OPTION%'=='7' GOTO Changelog
IF /I '%OPTION%'=='q' GOTO quit
IF /I '%OPTION%'=='quit' GOTO quit
IF /I '%OPTION%'=='b' GOTO menu
IF /I '%OPTION%'=='back' GOTO menu
ECHO.
ECHO ============INVALID INPUT============
ECHO Please select a number
ECHO or select 'Q' or 'B'.
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE > NUL
GOTO Options
:Update
ECHO.
IF EXIST "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat" (
GOTO Update_1
) ELSE (
ECHO You have not installed the script yet. Please install the script first.
)
PAUSE
GOTO Options
:Update_1
IF EXIST "%userprofile%\AppData\Local\Temp\Update.bat" (
GOTO Update_2
) ELSE (
REM Here the main script writes an update script to a batch file
ECHO DEL "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat" > "%userprofile%\AppData\Local\Temp\Update.bat"
REM I replaced the original file location with one on the desktop. The original location was on a network drive, location is confidential.
ECHO COPY "%userprofile\Desktop\Shortcut Browser *.bat" "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup" >> "%userprofile%\AppData\Local\Temp\Update.bat"
ECHO CALL "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat" >> "%userprofile%\AppData\Local\Temp\Update.bat"
ECHO EXIT >> "%userprofile%\AppData\Local\Temp\Update.bat"
)
GOTO Update_2
REM Here the main script calls upon the update script
:Update_2
CALL "%userprofile%\AppData\Local\Temp\Update.bat"
REM The script seems to hang here. Either both the scripts will remain open or close or a combination depending on where CALL and START were used.
PAUSE
GOTO Options
I saw elsewhere I could have used the FC command. I didn't know about it until troubleshooting this problem. I've gotten this far and I feel I'm just missing some small trick to get the new updated script to launch.
If you need any more info let me know. I think I covered everything.
#ECHO OFF
SETLOCAL
TITLE Super Script 5000
:Options
REM I'll set the variable here to what is set elsewhere in the script.
SET VERSION=Version 1.7
CLS
ECHO.
ECHO.
ECHO %VERSION%
ECHO.
ECHO.
ECHO ==================== OPTIONS =====================
ECHO.
ECHO 1. Update Script WIP
ECHO 2. Install entire script to Statup Folder
ECHO 3. Install individual items for auto-launch
ECHO 4. Install Windows Logout feature
ECHO 5. Uninstall
ECHO 6. Change Colors
ECHO 7. Changelog
ECHO.
ECHO =======PRESS 'Q' TO QUIT OR 'B' TO GO BACK========
ECHO.
SET "OPTION="
SET /P "OPTION=Select an Option: "
IF /I "%OPTION%"=="1" GOTO Update
IF /I "%OPTION%"=="2" GOTO Install
IF /I "%OPTION%"=="3" GOTO I_Install
IF /I "%OPTION%"=="4" GOTO I_Logout
IF /I "%OPTION%"=="5" GOTO Uinstall
IF /I "%OPTION%"=="6" GOTO Colors
IF /I "%OPTION%"=="7" GOTO Changelog
IF /I "%OPTION%"=="q" GOTO quit
IF /I "%OPTION%"=="quit" GOTO quit
IF /I "%OPTION%"=="b" GOTO menu
IF /I "%OPTION%"=="back" GOTO menu
ECHO.
ECHO ============INVALID INPUT============
ECHO Please select a number
ECHO or select 'Q' or 'B'.
ECHO ======PRESS ANY KEY TO CONTINUE======
PAUSE > NUL
GOTO Options
:Update
ECHO.
IF EXIST "%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat" (
GOTO Update_1
) ELSE (
ECHO You have not installed the script yet. Please install the script first.
)
PAUSE
GOTO Options
:Update_1
IF EXIST "%tmp%\update.bat" (
2>NUL DEL "%tmp%\update.bat" || GOTO Options
)
SETLOCAL
FOR %%A IN (
"%appdata%\Microsoft\Windows\Start Menu\Programs\Startup\Shortcut Browser *.bat"
) DO (
SET "old_version=%%~A"
SET "old_dir=%%~dpA"
SET "old_file=%%~nxA"
)
FOR %%A IN (
"%userprofile%\Desktop\Shortcut Browser *.bat"
) DO (
SET "new_version=%%~A"
SET "new_dir=%%~dpA"
SET "new_file=%%~nxA"
)
IF NOT DEFINED old_version (
ENDLOCAL
GOTO Options
) ELSE IF NOT DEFINED new_version (
ENDLOCAL
GOTO Options
)
(
ECHO #ECHO OFF
ECHO SETLOCAL
ECHO.
ECHO ECHO Update and restarting...
ECHO ^>NUL PING localhost -n 3
ECHO.
ECHO SET "old_version=%old_version%"
ECHO SET "new_version=%new_version%"
ECHO SET "updated_version=%old_dir%\%new_file%"
ECHO.
ECHO CALL :log /clear_log
ECHO.
ECHO IF NOT EXIST "%%new_version%%" (
ECHO CALL :log ERROR: Not exist "%%new_version%%".
ECHO EXIT /B 1
ECHO ^)
ECHO.
ECHO CALL :log Move old_version to old_version.bak.
ECHO MOVE "%%old_version%%" "%%old_version%%.bak" ^|^| (
ECHO CALL :log ERROR: Move failed to backup old version.
ECHO EXIT /B 2
ECHO ^)
ECHO.
ECHO CALL :log Copy new_version to old_version.
ECHO COPY "%%new_version%%" "%%appdata%%\Microsoft\Windows\Start Menu\Programs\Startup\" ^|^| (
ECHO CALL :log ERROR: Copy failed. Restore old version.
ECHO MOVE "%%old_version%%.bak" "%%old_version%%" ^|^| (
ECHO CALL :log ERROR: Restore old version failed.
ECHO EXIT /B 3
ECHO ^)
ECHO EXIT /B 4
ECHO ^)
ECHO.
ECHO CALL :log Delete old_version.bak.
ECHO DEL "%%old_version%%.bak" ^|^| (
ECHO CALL :log WARNING: Delete backup file failed.
ECHO ^)
ECHO.
ECHO IF NOT EXIST "%%updated_version%%" (
ECHO CALL :log ERROR: Not exist: "%%updated_version%%"
ECHO EXIT /B 5
ECHO ^)
ECHO.
ECHO ENDLOCAL
ECHO.
ECHO ECHO Ready to restart updated version
ECHO PAUSE
ECHO CALL "%old_dir%\%new_file%"
ECHO EXIT /B
ECHO.
ECHO :log
ECHO SETLOCAL
ECHO SET "log=%%tmp%%\update.log"
ECHO IF /I "%%~1"=="/clear_log" TYPE NUL ^> "%%log%%" ^& EXIT /B
ECHO IF /I "%%~1"=="/delete_log" 2^>NUL DEL "%%log%%" ^& EXIT /B
ECHO SET args=%%*
ECHO IF NOT DEFINED args EXIT /B
ECHO ECHO %%*
ECHO ^>^> "%%log%%" ECHO %%*
ECHO EXIT /B
) > "%tmp%\update.bat"
ENDLOCAL
START "Super Script 5000" "cmd /c "%tmp%\update.bat""
EXIT
:Quit
Tested on a Win7 VM. The script needs to restart on update else
weird issues may occur. CMD does not like scripts changing as it
is reading and interpreting them. Even reusing the same console
using START with /B may cause key input issues.
update.bat is overwritten every update as it stores absolute paths
so each update will be different paths with the filenames changing.
Using a wildcard with CALL may not work too well otherwise.
update.bat moves the old version to a .bak file, copies the new
version and if successful, deletes the .bak file, else moves the
.bak file to the old version. This is just to ensure a current
version always exists even if the update fails.
Changed quotes in :Options label from ' to ". Quotes are not
interchangeable like some other languages. Example: "%OPTION%"
will work if contains a space though '%OPTION%' may cause error.
I notice no SETLOCAL at the top of your full script. Advise you add
it unless you have a good reason not to.
I merged labels :Update_1 and :Update_2 into one.
Perhaps adjust Ping localhost -n number if more or less time is
needed.
Look at the %tmp%\update.log if a problem happens. Though expect
the errors to show in the console as the label :log in
update.bat echoes to console and writes to the log.
Related
I created a custom CMD Command with a Batch Script but I dont know how to make more than 1 Option...
What I have now is a Command which looks like this:
yce -update
But I want to make a Command where you can specify what is Updated which works like this:
yce -update (all, appname, eb)
This is the entire code:
#echo off
SET "parameter=%~1"
if /i "%parameter%" equ "" goto :yce_help
for %%p in (help h update fiddler ) do (
if /i "%parameter:~1%" equ "%%~p" goto :%%~p
rem if /i "%parameter%" equ "%%~p" goto :%%~p
)
goto :wrong_parameter
:wrong_parameter
echo yce: option -%parameter:~1%: is unknown
echo yce: try 'yce -help' for more information
goto :eof
:yce_help
echo yce: try 'yce -help' for more information
goto :eof
:h
goto :help
:help
echo.
echo.
echo.
echo Usage: yce [options...]
echo -update, Updates yCE
goto :eof
:update
echo [94mCreating Folders...[0m
md "%userprofile%\.yce"
md "%userprofile%\.yce\update"
echo [94mDownloading Latest Update...[0m
echo THIS SCRIPT DOWNLOADS THE UPDATE
echo [94mStarting Update...[0m
start %userprofile%\.yce\update\yce_update.bat
goto :eof
How do I make a log file from this code. This has been answered a ton, but this file is huge and I want a simple log file. As you see I also tried to add a log file names RandomNamesLog.log. See if the output goes there.
I want all the code to be outputted into the log file, with error messages and confirm messages, like removing the #ECHO OFF value.
If you need more on what I want, please tell me.
#ECHO OFF
SETLOCAL EnableDelayedExpansion for /F "tokens=1,2 delims=#" %%a in
('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do ( set
"DEL=%%a" )
ECHO Random Names
ECHO Written By: Trevor T
ECHO UltimateGuidesPro.blogspot.com
ECHO.
ECHO.
REM Randomly renames every file in a directory.
SETLOCAL EnableExtensions EnableDelayedExpansion
REM 0 = Rename the file randomly.
REM 1 = Prepend the existing file name with randomly generated string.
SET PrependOnly=0
REM 1 = Undo changes according to the translation file.
REM This will only work if the file "__Translation.txt" is in the same
folder.
REM If you delete the translaction file, you will not be able to undo
the changes!
SET Undo=0
REM --------------------------------------------------------------------------
REM Do not modify anything below this line unless you know what you are doing.
REM --------------------------------------------------------------------------
SET TranslationFile=_TranslatonData.txt
SET LogFile=_RandomNamesLog.log
IF NOT {%Undo%}=={1} (
REM Rename files
ECHO You are about to randomly rename every file in the following folder:
ECHO %~dp0
ECHO.
ECHO A file named %TranslationFile% will be created which allows you to undo this.
ECHO Warning: If %TranslationFile% is lost/deleted, this action cannot be undone.
ECHO Type "OK" to continue.
SET /P Confirm=
IF /I NOT {!Confirm!}=={OK} (
ECHO.
ECHO Aborting.
GOTO :EOF
)
ECHO Original Name/Random Name > %TranslationFile%
ECHO ------------------------- >> %TranslationFile%
FOR /F "tokens=*" %%A IN ('DIR /A:-D /B') DO (
IF NOT %%A==%~nx0 (
IF NOT %%A==%TranslationFile% (
SET Use=%%~xA
IF {%PrependOnly%}=={1} SET Use=_%%A
SET NewName=!RANDOM!-!RANDOM!-!RANDOM!!Use!
ECHO %%A/!NewName!>> %TranslationFile%
RENAME "%%A" "!NewName!"
ECHO ----------------------------------------------------- >> %TranslationFile%
ECHO Designed by Trevor T | UltimateGuidesPro.blogspot.com >> %TranslationFile%
ECHO. >> %TranslationFile%
ECHO WARNING: THIS FILE REQUIRED TO UNDO RANDOM NAMING! THE FILE CAN BE MOVED AS LONG AS IT IS IN THE FOLDER WHEN USING THE UNDO RANDOM NAMES FUNCTION! >> %TranslationFile%
ECHO +---------+
ECHO | Credits |
ECHO +---------+
Echo.
ECHO Original Random Names script created by
call :colorEcho 0a "Jason Faulkner"
call :colorEcho 0a "HowToGeek.com"
ECHO ---------------------------------------
ECHO Script created by
call :colorEcho 0a "Trevor T"
ECHO -------------------------------------------
ECHO Color Text system created by
call :colorEcho 0a "Visual Magic, user on Stack Exchange"
ECHO Type "Exit" to quit
GOTO :Exit
:Exit
SET /P Confirm=
IF /I NOT {!Confirm!}=={Exit} (
ECHO.
ECHO Please try again
GOTO :Exit2
Exit
:Exit2
SET /P Confirm=
IF /I NOT {!Confirm!}=={OK} (
ECHO.
ECHO Please try again
GOTO :Exit
)
)
)
) ELSE (
ECHO UNDO MODE
IF NOT EXIST %TranslationFile% (
ECHO Missing translation file: %TranslationFile%
ECHO Please make sure %TranslationFile% is in the location of the random files. Otherwise it will give this error.
PAUSE
GOTO :EOF
)
ECHO Undoing...
FOR /F "skip=2 tokens=1,2 delims=/" %%A IN (%TranslationFile%) DO RENAME "%%B" "%%A"
DEL /F /Q %TranslationFile%
ECHO Completed Undo!
Pause
)
Exit
REM This is the color text code. Don't touch this, unless you know a better way to use color text.
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
I have rewritten your script fixing all of the problems I could see and removing the pointless code to add colored text.
Test it to see if it works as it should adding whatever you think I've missed.
#ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
ECHO Random Names
ECHO Written By: Trevor T
ECHO UltimateGuidesPro.blogspot.com
ECHO.
REM Randomly renames every file in a directory.
REM 0 = Rename the file randomly.
REM 1 = Prepend the existing file name with randomly generated string.
SET "PrependOnly=0"
REM 1 = Undo changes according to the translation file.
REM This will only work if the file "_TranslationData.txt" is in the same folder.
REM If you delete the translaction file, you will not be able to undo the changes!
SET "Undo=0"
REM --------------------------------------------------------------------------
REM Do not modify anything below this line unless you know what you are doing.
REM --------------------------------------------------------------------------
SET "TranslationFile=_TranslationData.txt"
IF "%Undo%"=="1" GOTO UNDOIT
ECHO You are about to randomly rename every file in the following folder:
ECHO %CD%
ECHO.
CHOICE /M "Do you want to continue "
IF NOT %ERRORLEVEL%==1 (
ECHO.
ECHO Aborting.
TIMEOUT -1
GOTO :EOF
)
ECHO A file named %TranslationFile% will be created which allows you to undo this.
ECHO Warning: If %TranslationFile% is lost/deleted, this action cannot be undone.
(
ECHO ;Original Name/Random Name
ECHO ;-------------------------
FOR %%A IN (*) DO IF NOT "%%A"=="%~nx0" IF NOT "%%A"=="%TranslationFile%" (
SET "Use=%%~xA"
IF "%PrependOnly%"=="1" SET "Use=_%%A"
SET "NewName=!RANDOM!-!RANDOM!-!RANDOM!!Use!"
ECHO %%A/!NewName!
REN "%%A" "!NewName!"
)
ECHO ;-----------------------------------------------------
ECHO ;Designed by Trevor T ^| UltimateGuidesPro.blogspot.com
ECHO.
ECHO ;WARNING: THIS FILE REQUIRED TO UNDO RANDOM NAMING!
ECHO ; THE FILE CAN BE MOVED AS LONG AS IT IS IN THE FOLDER WHEN USING THE UNDO RANDOM NAMES FUNCTION!
)>"%TranslationFile%"
ECHO +---------+
ECHO ^| Credits ^|
ECHO +---------+
ECHO.
ECHO Original Random Names script created by Jason Faulkner # HowToGeek.com
ECHO -------------------------------------------
ECHO Script created by Trevor T
ECHO -------------------------------------------
TIMEOUT -1
GOTO :EOF
:UNDOIT
ECHO UNDO MODE
IF NOT EXIST "%TranslationFile%" (
ECHO Missing translation file: %TranslationFile%
ECHO Please make sure %TranslationFile% is in the location of the random files. Otherwise it will give this error.
TIMEOUT -1
GOTO :EOF
)
ECHO Undoing...
FOR /F "USEBACKQ TOKENS=1-2 DELIMS=/" %%A IN ("%TranslationFile%") DO REN "%%B" "%%A"
DEL /F "%TranslationFile%"
ECHO Completed Undo!
TIMEOUT -1
GOTO :EOF
Once you are happy with it edit your opening question explaining what it is you want to have in the log file and how you need it to look.
I use this. it seems to work.
It just starts a log file and maintains logs.
You can set logpath and logname (base name) then you call this create_logfile
It creates a semi-random log file.
Now you write to the set logfile
I'm not an advanced coder. Please feel free to correct:
#echo off
rem
echo %date% %TIME%
rem Log File Path
if "%logpath%" == "" set logpath=%~dp0
if "%logpath:~-1%" == "\" set logpath=%logpath:~0,-1%
rem Make log file name
if "%logname%" == "" set logname=log
set t=%TIME: =0%
set d=%DATE%
set logfilename=%logname%_%d:~12,2%%d:~4,2%%d:~7,2%%t:~0,2%%t:~3,2%%t:~6,2%.log
set t=
set d=
rem Test
echo full log file name = %logfilename%
echo find base log name = %logfilename:~0,-17%
echo log path = %logpath%
rem Logs to maintain
set /a dellog=5
rem Find file to delete
set var=
set /a cnt=0
set fltodel=
set fstfle=
for %%a in (%logpath%\%logname%*.log) do (
set var=%%a
call set /a "cnt+=1"
echo %%cnt%%| findstr /l "1" >nul && (call set fstfle=%%var%%)
echo %%cnt%%| findstr /l "%dellog%" >nul && (call set fltodel=%%fstfle%%)
)
rem Delete old log
echo file to delete %fltodel%
if exist %fltodel% (del /q %fltodel%) else (echo "%fltodel%" does not exist. nothing to delete.)
echo tryed to delete
rem Create Log File and logfile attribute
set logfile=%logpath%\%logfilename%
if exist %logfile% (
echo start logging... "%~0" on %date% %time% >> %logfile%
) ELSE (
echo Start Log for "%~0" on %date% %time% > %logfile%
)
I made a simple BATCH file for notes, it just deletes a folder, makes a new one, and puts in a file called note.txt and then edits it... for some reason, it doesn't do ANYTHING at all! not even removing the folder, here's the code:
goto start
:abort
ECHO Aborted!
PAUSE
GOTO start
:replace
del /q "D:\Users\Eldar\Desktop\Note Folder"
mkdir "Note Folder"
#echo>"D:\Users\Eldar\Desktop\Note Folder\note.txt"
#echo %note%> "D:\Users\Eldar\Desktop\Note Folder\note.txt"
pause
:start
#ECHO OFF
color e
cls
echo Put the note in here:
set /p LastNote=<"D:\Users\Eldar\Desktop\Note Folder\note.txt"
SET /P note=
cls
echo Note has been saved!
echo also, the last note you saved was: "%LastNote%".
echo so are you sure you want to replace this note with a new one (Y/N)?
SET /P agree=
IF "%agree%"=="y" (
ECHO goto replace
) ELSE (
GOTO abort
)
There are few errors in your script:
del can delete only empty folders. Use rmdir /s /q folder_name to remove folder with files
Remove ECHO in ECHO goto replace
You are using full paths everywhere except mkdir. Is it correct?
My working version:
#echo off
goto start
:abort
echo Aborted!
pause
goto start
:replace
rmdir /s /q ".\TestF"
mkdir "TestF"
echo>".\TestF\note.txt"
echo %note%> ".\TestF\note.txt"
pause
:start
color e
cls
echo Put the note in here:
set /p LastNote=<".\TestF\note.txt"
set /p note=
cls
echo Note has been saved!
echo also, the last note you saved was: "%LastNote%".
echo so are you sure you want to replace this note with a new one (Y/N)?
set /p agree=
if "%agree%"=="y" (
goto replace
) else (
goto abort
)
There appears to be no reason to remove the Note Folder directory or even to delete the note.txt file.
How does it perform if you rewrite it like this?
#Echo Off
Color 0E
Set "LastNote="
If Exist "%UserProfile%\Desktop\Note Folder\note.txt" (
Set/P LastNote=<"%UserProfile%\Desktop\Note Folder\note.txt"
)
If Not Defined LastNote GoTo replace
:ask
ClS
Echo=The last note you saved was:
Echo=%LastNote%
Echo=
Choice /M "Would you like to replace this note with a new one"
If "%ErrorLevel%"=="1" GoTo replace
Echo=
Echo=Aborted!
Timeout -1
GoTo ask
:replace
ClS
Set/P "note=Put the note in here: "
If "%note%"=="" GoTo ask
Echo=Note has been saved!
If Not Exist "%UserProfile%\Desktop\Note Folder\" (
MD "%UserProfile%\Desktop\Note Folder"
)
(Echo=%note%)>"%UserProfile%\Desktop\Note Folder\note.txt"
Timeout -1
GoTo ask
For the benefit of the OP, here is your version of your script with the major flaws removed and with unnecessary lines kept in.
#goto start
:abort
ECHO Aborted!
PAUSE
GOTO start
:replace
rmdir "D:\Users\Eldar\Desktop\Note Folder"
mkdir "D:\Users\Eldar\Desktop\Note Folder"
echo.>"D:\Users\Eldar\Desktop\Note Folder\note.txt"
>"D:\Users\Eldar\Desktop\Note Folder\note.txt" echo %note%
pause
:start
#ECHO OFF
color e
cls
echo Put the note in here:
set /p LastNote=<"D:\Users\Eldar\Desktop\Note Folder\note.txt"
SET /P note=
cls
echo Note has been saved!
echo also, the last note you saved was: "%LastNote%".
echo so are you sure you want to replace this note with a new one (Y/N)?
SET /P agree=
IF "%agree%"=="y" (
goto replace
) ELSE (
GOTO abort
)
I have written the following synchronization code batch to flush all data inside a master hard drive into a backup drive. Because of drives name can change I used a trick putting master.txt into source drive and backup.txt into backup drive. In this way I can easily find source and backup. Omn windows 7 it works pretty well but once inside WinCE the hell.
cls
::#echo off
PATH=\hard disk;\hard disk2;\hard disk3;\hard disk4;e:;f:;g:
for %%A in ("%path:;=";"%") do (
if exist %%~A\master.txt (
echo found %%~A\master.txt
SET data=%%~A\Rilevamenti
)
if exist %%~A\backup.txt (
echo found %%~A\backup.txt
SET backup=%%~A
)
)
echo source path is: %data%
:logfile
Time /T > Time.dat
SET /P ftime= < Time.dat
SET DirName=BackupFiles%date:~6%%date:~3,2%%date:~0,2%%ftime:~0,2%%ftime:~3,2%
SET backup=%backup%\%DirName%
echo backup dir is: %backup%
pause
mkdir "%backup%"
echo 1 > LogSyncFile_src
echo 1 > LogSyncFile_dst
SET COPYCMD=/Y
for /R "%data%" %%F in (*.CSV) do (
move %%F %backup%\%%~nxF
echo %%~nxF >> LogSyncFile_src
)
for /R "%backup%" %%F in (*.CSV) do (
echo %%~nxF >> LogSyncFile_dst
)
SET /P Build=<LogSyncFile_src
SET /P Synch=<LogSyncFile_dst
if %Build%==%Synch% (
goto :delete
) else (
goto :fail
)
:fail
echo synchronization failed!
goto :exit
:lowmemory
echo Insufficient memory to copy files or
echo invalid drive or command-line syntax.
goto :exit
:abort
echo You pressed CTRL+C to end the copy operation.
goto :exit
:delete
echo synchronization completed!
goto :exit
:exit
pause
On WinCE for the first loop I get:
for %%A in ("%path:;=";"%") do ( Cannot execute for.exe
Any help on what's going on? It is the second time I write the same batch in a different way to get working on WinCE as well but it seems I am missing something...
About 3 days ago I asked a question, which can be found here:
how to replace a string on the second line in a text file using a batch file?
I'm converting the letters in a text file into their respective numbers. I'm getting error messages such as "Access denied" and "Cannot locate this file" -- but the same batch file that's giving me all these errors is also the one that made these text files to begin with! So it should be in the same directory as the batch file itself (unless specified otherwise), right? I even went to that folder and checked, and they're there.
I did add a small script to hide the files after they were created so that it wouldn't look so cluttered up. I did this by using
attrib +h C:\script\%name%.txt
Would hiding a file with this command make it invisible to batch programs that are searching for it/call upon it?
Here's a link to the file, "stringparsing.bat": http://uploading.com/files/a1m1d2f4/stringparsing.bat/
If you could assist me in getting this program to carry out its task without any errors it would be greatly appreciated!
Here's the "stringparsing.bat" file in full:
#echo off
setlocal enabledelayedexpansion
title BETA
cls
cd C:\script\st
echo.
echo.
echo.
echo Setting Variables...
echo Loading Language Database...
:: ###################################################################################
:: CALLING VARIABLE DATABASE CALLING VARIABLE DATABASE CALLING VARIABLE DATABASE
:: ###################################################################################
TIMEOUT /t 5 /nobreak > nul
goto MAIN
:MAIN
set foo=0
cls
echo.
echo.
echo.
echo.
echo ===================================
echo #################################
echo ####### Main Menu: #######
echo #################################
echo ===================================
echo.
echo.
echo 1.) Create New Language File...
echo.
echo 2.) Load Existing Lanuage File...
echo.
echo 3.) Settings...
echo ---------------------------------------------------------
SET /p CHOICE= Select a Function:
IF %CHOICE%== 1 GOTO CREATE
IF %CHOICE%== 2 GOTO LOAD
IF %CHOICE%== 3 GOTO SETTINGS
GOTO MAIN
:CREATE
cls
title Step 1
echo.
echo.
echo.
echo =================================================================================
echo.
set /p name= please type a name for your new language file:
echo.
echo =================================================================================
cls
echo. > %name%.txt
echo.
echo.
echo.
echo ==============================================================
echo ##############################################################
echo #============================================================#
echo # #
echo # - After you hit enter you will be redirected #
echo # to a Live Typer. so anything you type into #
echo # it will be sent to %name%.txt. #
echo # #
echo # #
echo # - Next, select load language File For Encoding! #
echo # #
echo #============================================================#
echo ##############################################################
echo ==============================================================
set /p line1= :
echo %line1% >> %name%.txt 2> nul
echo %name% > Language_File.txt
attrib +h Language_File.txt
set /a foo+ =1
)
echo.
echo ==========================================================
goto LOAD
:LOAD
set /a foo+ =1
IF %foo%== 2 goto loadexternal
goto LOAD23
:loadexternal
echo.
echo language file is loading now!
set /p name=<Language_File.txt
timeout /t 4 /nobreak > nul
echo.
echo.
echo Language_File Loaded!
pause >nul
goto LOAD23
:LOAD23
cls
echo.
echo.
echo.
echo.
echo.
echo Encoding Your Language File... Please Wait...
echo.
echo.
echo.
for /f "delims=" %%i in (!name!.txt) do (
echo translating "%%i"
set var=%%i
set var=!var:a=1 !
set var=!var:b=2 !
set var=!var:c=3 !
set var=!var:d=4 !
set var=!var:e=5 !
set var=!var:f=6 !
set var=!var:g=7 !
set var=!var:h=8 !
set var=!var:i=9 !
set var=!var:j=10 !
set var=!var:k=11 !
set var=!var:l=12 !
set var=!var:m=13 !
set var=!var:n=14 !
set var=!var:o=15 !
set var=!var:p=16 !
set var=!var:q=17 !
set var=!var:r=18 !
set var=!var:s=19 !
set var=!var:t=20 !
set var=!var:u=21 !
set var=!var:v=22 !
set var=!var:w=23 !
set var=!var:x=24 !
set var=!var:y=25 !
set var=!var:z=26 !
echo !var!
)
echo !var! > !name!.txt
pause >nul
TIMEOUT /t 5 /nobreak > nul
goto MAIN
:END
cls
title SHUTTING DOWN...
echo.
echo.
echo.
echo Terminating service stream...
echo.
echo.
echo.
echo.
echo Done! Thank you for using this program!
TIMEOUT /t 5 /nobreak > nul
::(%xx%) -1 I/O Stream= "SHELL.dll"
:: IF EXIST [&1[Parser_2009]] exit
Exit
:: #####################################################################################
You've got a few problems. First, the access denied problem is from you redirecting to a hidden file.
echo %name% > Language_File.txt
attrib +h Language_File.txt
Note that the first time you run the script, it will work because Language_File.txt won't exist and therefore won't be hidden. The second time you run it, you'll get access denied. I don't know why Windows doesn't let you do that. You can solve this problem in a couple ways.
1. Save your file to the user's temp directory. With this approach your directory won't get cluttered.
echo %name% > %TMP%\Language_File.txt
2. Save your file to a subdirectory that you own so that it doesn't clutter the script's directory.
if not exist workspace mkdir workspace
echo %name% > workspace\Language_File.txt
3. Unhide the file before you use it. Since the file may not exist the first time you run the script, perhaps you should only attrib -h if it exists.
if exist Language_File.txt attrib -h Language_File.txt
echo %name% > %TMP%\Language_File.txt
attrib +h Language_File.txt
4. Don't use Language_File.txt at all! I don't see why you need it. Just use variables to hold the name of the language file. In fact, you already have the name in %name%, right?
Second, you should check the value of your variables to see what they really hold. When you load the contents of Language_File.txt into your variable, it's loading all the contents. That includes the hidden newline characters \r\n, although the script seems to bring them into the variable as spaces. See:
c:\batch\t>echo language file is loading now!
language file is loading now
C:\batch\t>set /p name= <Language_File.txt
C:\batch\t>echo -%name%-
-langfile -
If you echo %name% surrounded by hyphens, you can see that there are 2 spaces after it from (presumably) the newline characters. To solve this problem, you can use set to trim the trailing characters.
C:\batch\t>echo language file is loading now!
language file is loading now
C:\batch\t>set /p name= <Language_File.txt
C:\batch\t>set name=%name:~0,-2%
C:\batch\t>echo -%name%-
-langfile-
In the second example, `%name% doesn't have the hidden characters.
Finally, you only need to use ! to access variables that you set inside the for loop. So all references to !name! should be %name% instead. That's probably your "cannot find file" error.
heres the "stringparsing.bat" file:
#echo off
setlocal enabledelayedexpansion
title BETA
cls
cd C:\script\st
echo.
echo.
echo.
echo Setting Variables...
echo Loading Language Database...
:: ###################################################################################
:: CALLING VARIABLE DATABASE CALLING VARIABLE DATABASE CALLING VARIABLE DATABASE
:: ###################################################################################
TIMEOUT /t 5 /nobreak > nul
goto MAIN
:MAIN
set foo=0
cls
echo.
echo.
echo.
echo.
echo ===================================
echo #################################
echo ####### Main Menu: #######
echo #################################
echo ===================================
echo.
echo.
echo 1.) Create New Language File...
echo.
echo 2.) Load Existing Lanuage File...
echo.
echo 3.) Settings...
echo ---------------------------------------------------------
SET /p CHOICE= Select a Function:
IF %CHOICE%== 1 GOTO CREATE
IF %CHOICE%== 2 GOTO LOAD
IF %CHOICE%== 3 GOTO SETTINGS
GOTO MAIN
:CREATE
cls
title Step 1
echo.
echo.
echo.
echo =================================================================================
echo.
set /p name= please type a name for your new language file:
echo.
echo =================================================================================
cls
echo. > %name%.txt
echo.
echo.
echo.
echo ==============================================================
echo ##############################################################
echo #============================================================#
echo # #
echo # - After you hit enter you will be redirected #
echo # to a Live Typer. so anything you type into #
echo # it will be sent to %name%.txt. #
echo # #
echo # #
echo # - Next, select load language File For Encoding! #
echo # #
echo #============================================================#
echo ##############################################################
echo ==============================================================
set /p line1= :
echo %line1% >> %name%.txt 2> nul
echo %name% > Language_File.txt
attrib +h Language_File.txt
set /a foo+ =1
)
echo.
echo ==========================================================
goto LOAD
:LOAD
set /a foo+ =1
IF %foo%== 2 goto loadexternal
goto LOAD23
:loadexternal
echo.
echo language file is loading now!
set /p name=<Language_File.txt
timeout /t 4 /nobreak > nul
echo.
echo.
echo Language_File Loaded!
pause >nul
goto LOAD23
:LOAD23
cls
echo.
echo.
echo.
echo.
echo.
echo Encoding Your Language File... Please Wait...
echo.
echo.
echo.
for /f "delims=" %%i in (!name!.txt) do (
echo translating "%%i"
set var=%%i
set var=!var:a=1 !
set var=!var:b=2 !
set var=!var:c=3 !
set var=!var:d=4 !
set var=!var:e=5 !
set var=!var:f=6 !
set var=!var:g=7 !
set var=!var:h=8 !
set var=!var:i=9 !
set var=!var:j=10 !
set var=!var:k=11 !
set var=!var:l=12 !
set var=!var:m=13 !
set var=!var:n=14 !
set var=!var:o=15 !
set var=!var:p=16 !
set var=!var:q=17 !
set var=!var:r=18 !
set var=!var:s=19 !
set var=!var:t=20 !
set var=!var:u=21 !
set var=!var:v=22 !
set var=!var:w=23 !
set var=!var:x=24 !
set var=!var:y=25 !
set var=!var:z=26 !
echo !var!
)
echo !var! > !name!.txt
pause >nul
TIMEOUT /t 5 /nobreak > nul
goto MAIN
:END
cls
title SHUTTING DOWN...
echo.
echo.
echo.
echo Terminating service stream...
echo.
echo.
echo.
echo.
echo Done! Thank you for using this program!
TIMEOUT /t 5 /nobreak > nul
::(%xx%) -1 I/O Stream= "SHELL.dll"
:: IF EXIST [&1[Parser_2009]] exit
Exit
:: #####################################################################################
I Finally learned how to format the code snippet.
(heres a link to another copy of it if you need it.)
how to replace a string on the second line in a text file using a batch file?