Cmd / Batch moving files from one location to another while creating logs on error - batch-file

Moving files from one location to another while creating logs on error is not creating the txt file, and I dont see why...
cls
echo %~n0%~x0 wird ausgefuehrt...
Rem zum Ändern der Pfade bzw. Dokumente:
rem Vom Pfad:
SET Drive="\\vf09.it.volvo.se\de32\PDF\01\"
rem Dateiart: Wichtig! * makiert, als wildcard, ein bliebigen Namen
SET docTyp="*.pdf"
rem Ziel Verzeichnis
SET Ziel="D:\BCT\Import\Rechnungsausgang\"
SET MaxSize=20971520
if not exist %Drive% (
goto driveError
) else (
echo Pfad gefunden.
goto checkZiel
)
:checkZiel
if exist %Ziel% (
echo Zielpfad gefunden.
goto checkFiles
) else (
goto ZielError
)
:checkFiles
if exist "%Drive%%docTyp%" (
goto continue
) else (
goto error
)
:ZielError
if not exist "Logs\" mkdir Logs
if not exist "Logs\%~n0.txt" type nul %~n0.txt
for /F "usebackq" %%A IN ('Logs\%~n0.txt') DO set LogSize=%%~zA
if %LogSize% GTR %MaxSize% ren Logs\%~n0.txt "%date%-%time:~0,2%_%time:~3,2%-%~n0.txt"
echo %date% %time:~0,5% - Das Ziel-Verzeichnis bzw. der Pfad %Ziel% konnte nicht gefunden werden! >> Logs\%~n0.txt
goto eof
:driveError
if not exist "Logs\" mkdir Logs
if not exist "Logs\%~n0.txt" type nul %~n0.txt
for /F "usebackq" %%A IN ('Logs\%~n0.txt') DO set LogSize=%%~zA
if %LogSize% GTR %MaxSize% ren Logs\%~n0.txt "%date%-%time:~0,2%_%time:~3,2%-%~n0.txt"
echo %date% %time:~0,5% - Das Verzeichnis bzw. der Pfad %Drive% konnte nicht gefunden werden! >> Logs\%~n0.txt
goto eof
:error
if not exist "Logs\" mkdir Logs
if not exist "Logs\%~n0.txt" type nul %~n0.txt
for /F "usebackq" %%A IN ('Logs\%~n0.txt') DO set LogSize=%%~zA
if %LogSize% GTR %MaxSize% ren Logs\%~n0.txt "%date%-%time:~0,2%_%time:~3,2%-%~n0.txt"
echo %date% %time:~0,5% - Keine Dateien mit Endung "%docTyp:~2,4%" im Verzeichnis %Drive% gefunden. >> Logs\%~n0.txt
goto eof
:continue
echo %docTyp:~2,4%s gefunden:
xcopy %Drive%%docTyp% %Ziel%
if not exist "Backup\" mkdir Backup
move /Y %Drive%%docTyp% %Drive%\Backup
:eof
Just getting back into batch programming and I need to make a relative simple copy and move from files, but I seem to fail on error logging. Whilst, yes I know I could make it all in one, I find the jumps a bit easier for the start.
Either way that doesnt explain why it wont create the folder or the log file.
I am confused.
Output of console from call:
C:\Users\Timon>echo moving_files.bat wird ausgefuehrt...
moving_files.bat wird ausgefuehrt...
C:\Users\Timon>Rem zum Ändern der Pfade bzw. Dokumente:
C:\Users\Timon>rem Vom Pfad:
C:\Users\Timon>SET Drive="\\vf09.it.volvo.se\de32\PDF\01\"
C:\Users\Timon>rem Dateiart: Wichtig! * makiert, als wildcard, ein bliebigen Namen
C:\Users\Timon>SET docTyp="*.pdf"
C:\Users\Timon>rem Ziel Verzeichnis
C:\Users\Timon>SET Ziel="D:\BCT\Import\Rechnungsausgang\"
C:\Users\Timon>SET MaxSize=20971520
C:\Users\Timon>if not exist "\\vf09.it.volvo.se\de32\PDF\01\" (goto driveError ) else (
echo Pfad gefunden.
goto checkZiel
)
C:\Users\Timon>if not exist Logs\ mkdir Logs
C:\Users\Timon>if not exist "Logs\moving_files.txt" type nul moving_files.txt
C:\Users\Timon>for /F "usebackq" %A IN ('Logs\moving_files.txt') DO set LogSize=%~zA
C:\Users\Timon>set LogSize=1840
C:\Users\Timon>if 1840 GTR 20971520 ren Logs\moving_files.txt "13.02.2023-17_07-moving_files.txt"
C:\Users\Timon>echo 13.02.2023 17:07 - Das Verzeichnis bzw. der Pfad "\\vf09.it.volvo.se\de32\PDF\01\" konnte nicht gefunden werden! 1>>Logs\moving_files.txt
C:\Users\Timon>goto eof
That's all I get.

Related

The file is renamed with batch file but it won't stop

I need to rename all files in a folder by adding "BV" in front of the files name. My code is functioning but the problem appears when the total amount of file in that folder is more than ca. 200 files. It will rename all files to this: "BV_BV_BV..." and it won't stop.
Here is my code:
#echo off & setlocal
ECHO Moechten Sie das wirklich tun? (Bereits vorhandene Bildverzeichnisse werden geloescht
ECHO und in den Dateinamen wird AWK2 hinzugefuegt) (j / n)
SET /p wahl=
if '%wahl%' == 'n' goto Nein
if '%wahl%' == 'j' goto Ja
:Ja
ECHO on
FOR %%I IN (*.bmp) DO ren "%%I" BV_"%%~nI".bmp
pause
I am not sure how I can make the batch file stop after all files have been renamed. Thanks for your help.
Assuming you want to rename all files once, with the prefix of BV_ as per your example in the for loop. Notice, I used choice instead of set /p
#echo off & setlocal
ECHO Moechten Sie das wirklich tun? (Bereits vorhandene Bildverzeichnisse werden geloescht
ECHO und in den Dateinamen wird AWK2 hinzugefuegt) (j / n)
choice /c jn /m "wahl"
goto opt%errorlevel%
:opt1
for /f "delims=" %%I IN ('dir /b /a-d *.bmp ^| findstr /v /b "BV_"') do ren "%%~I" "BV_%%~I"
goto :eof
:opt2
echo do what ever you need to do here.

Detecting a file update in BATCH

CLOSED STRING DUE TO LACK OF HELPFUL COMMENTS
I am attempting to write a slew of code that registers when a file is updated, then displays the updated file. I have tried many things and nothing is working. Is there a way to check for updates to a file, and then run a command if it does, and wait for a change if it does not?
UPDATE:
I realize I am not giving as much information as I thought I was.
I am creating a batch file that will run off of a local network drive that hosts a chat room. I have created a program where you have the chat box for input and a read box for the output. I then have two log files, one for the log of user login, and another for user messages.
I need the messages screen to update all of the users when someone posts a comment. I have it set up to reload every second, but it is a little eye-hurting.
Is there any way I can tell the read file to loop back once it indicates there is a change in the messages file?
Thank you guys for the help I have been recieved.
I appreciate any help. Tell me in case I missed anything I need to add.
You should use one file per user to check the status. This allows you to make different ads in your own window.
The chat window does not necessarily have to be shared - but it is possible to split it.
The display does not have to be updated again and again, it is activated only for writing and otherwise it shows the messages of other users or their own settings.
Status messages are displayed via the window title.
Last but not least, the messages are read out of a shared chat file using find or more. Of course you can do that with robocopy, but if that works fast enough ...
You can extend it all so that for each sent message a control file per user is created. This in turn would allow robocopy to easily monitor such a control file (s). then show the following chat history as usual in the cli and remove the control file (s). that should at least calm the batch a bit.
I have a very old chat program (I wrote it during a training session to chat with the others, I was bored).
It uses the downloadable choice.exe, which must be placed in the same directory as the script.
Sure, I would write it today quite differently, and so it works with xcopy instead of choice (keyword SnakeBatch). But I'm too lazy. I added some comments ...
And I also admit that this script is not well written - but it's just old, special and ugly. Since this script is already lying around for a while with me in the filing - I am of opinion I operate no script writing service (may the others forgive me). Yes it goes a little bit over the question, but what is it ...
#echo off
cls
setlocal
rem Vars: color text - backround
set "LC=f"
set "BC=0"
set "Full= "
color %BC%%LC%
set "NameSearch=%~n0"
set "NameSearch=%NameSearch:~,-1%?%~x0"
ver |find " XP " >nul && (set "UEB="
set "XP=/c:"::x [\!One!]""
)
ver |find " XP " >nul || set "UEB=1" &&set "XP=/c:"::v [\!One!]""
:: Freigabe schon verfügbar ?
::net use
::net share
mountvol|find /i ":\">"%temp%\test"
for %%i in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
find /i "%%i:\" "%temp%\test">nul || for /f "delims=" %%k in ('
" for /r "%%i:\" %%j in ("%Namesearch%") do if "%~nx0" == "%%~nxj" echo "%%j" "
') do set "LWShare=%%~dpk"
)
rem status file switch
:: Nur eine Datei davon maximal Vorhanden
:: --------------------------------------
:: Batch Chat User Angemeldet ".bca"
:: Batch Chat User Schreibt ".bcw"
:: Batch Chat User Offline ".bco" rem NOT in use: list old/unused names
:: Batch Chat User Idle ".bci"
::
rem status file beep
:: Batch Chat User Ring ".bcr"
rem NOT in use: only test file switch - private , timer
:::: nach der Anmeldung immer Vorhanden
:::: --------------------------------------
:::: Batch Chat User Timer ".bct"
:::: Batch Chat User Privat ".bcp"
rem color code
:: code Farbe Bedeutung Schrift H-grund
:: 0 Schwarz Andere Anwesend x
:: 8 D-Grau Allein Anwesend x
:: 1 D-Blau Anderer Benutzer schreibt x
:: 7 H-Grau Option Aus x
:: f Weis Option Ein/Eingabe Fertig x
:: c Rot Erkennung eines Befehles x
:: a Grün Benutzer schreibt x
:: b Zyan <Benutzer : Meldung> x
:: d Magenta Modus Idle x
:: e Gelb Nachricht Erhalten x
:: 2 D-Grün
:: 3 BlauGrün
:: 4 D-Rot
:: 5 Lila
:: 6 Ocker
:: 9 Blau
::goto :begin
:settings
rem idle message - file = .bci
rem write message - file = .bcw
rem self - idle info message
rem self - write info message
rem self - alone info message
set "IdleInfo=... liegt grad sinnlos herum"
set "WriteInfo=... schreibt gerade"
set "MyIdleInfo=... Ich mach grad eine 15"
set "MyWriteInfo=: Ich Schreibe gerade etwas ..."
set "AloneInfo=Du bist am Arsch der Welt"
:begin
if defined LWShare ( pushD "%LWShare%" ) else pushD "%~dp0"
rem Input variant choice.exe
if not exist "%temp%\choice.exe" copy /y /z choice.exe "%temp%\choice.exe" >nul||(echo Keine choice.exe im Verzeichnis vorhanden&pause&goto :eof)
title Chat for CMD-Line
echo Chat Server Laufwerk: "%cd%"
#echo off
set "Userin=%Username%"
echo\%Username%>"%temp%\test"
for /f "tokens=2 delims=. " %%i in ('" echo User 0& Dir /b /od "User *.bc?" 2>nul "') do (
for /f %%j in ('" set /a x=%%~ni + 1 "') do if not exist "User %%~j.*bc?" set "Userin=User %%j"
)
Dir /b "%username%.bc?" >nul 2>&1 && ( echo Sorry "%username%" Dein Name ist schon vergeben!
)
goto :Userin
rem NOT in use: method usernames - list old/unused names
:User
(for /f "delims=." %%i in ('" dir /b *.bco 2>nul && echo --- M”gliche Benutzernamen "') do echo %%i
)>"%temp%\file"
sort "%temp%\file"
:Userin
rem - list used usernames
(for /f "delims=." %%i in ('" dir /b *.bc? 2>nul |findstr /r /c:"[awi]$" && echo --- Belegte Benutzernamen "') do echo %%i
)>"%temp%\file"
sort "%temp%\file"
set "USER="
:Userin
rem username
set /p User="Waehle einen anderen Benutzernamen oder mit nur Enter waehle "%Userin%" "
setlocal enabledelayedexpansion
if not defined User set "User=%username%"
echo\!User!>"%temp%\test"
endlocal
findstr /v /r "[?.</>\\|:;%%=~*\[\]()&]" "%temp%\test"|findstr /v /r /c:"[ ]$"|findstr /v /r "^-" >nul || (cls & echo Nicht Verwertbare Zeichen gefunden!& goto :User )
if defined User findstr "[0-Z][0-Z][0-Z]" "%temp%\test" || echo Zu wenig Zeichen! Anderen Namen waehlen!&& Goto :User
if defined User Dir /b "%user%.bc?" "%user%.*.bc?" >nul 2>&1 && echo Benutzername "%User%" ist schon vergeben - anderen Namen waehlen!&& goto :User
if not defined User for /f "tokens=2 delims=. " %%i in ('" echo User 0& Dir /b /od "User *.bc?" 2>nul "') do if not defined User for /f %%j in ('" set /a x=%%~ni + 1 "') do if not exist "User %%~j.*bc?" set "User=User %%j"
#echo off
cls
for /f %%i in (' cmd /u /von /c"echo !user!"^|find /v ""^|findstr .^|find /c /v "" ') do call set "Free=%%Full:~%%i%%"
if not defined Free set "Free= "
if not exist Chat.log type nul > Chat.log
set "Spot=Echo\^<%User% : spot Befehl Erkennung^>"
set "Echo=1"
:ReFresh
set "LC=f"
set "BC=0"
color %BC%%LC%
setlocal enabledelayedexpansion
echo Hallo !user! ... Willkommen im Chat
for /f %%i in ('" Dir /b *.bc? 2>nul|findstr /v /r /c:"[.]bco$"|findstr /v /r /c:"^%User%[.]"|find /v /c "" 2>nul "') do if %%i equ 0 (echo Du bist der erste Hier! & if not defined Gr1 if exist Chat.log del Chat.log & type nul > Chat.log ) else echo Es sind bereits %%i Personen im Chat.&echo\&(for /f "delims=." %%i in ('" Dir /b /od *.bc? 2>nul|findstr /v /r /c:"^%User%[.]*bc"|findstr /r /c:"[.]bc[aiw]$" "') do echo %%~i)& echo und Du : !USER!
echo\&echo Zum Abmelden EXIT eingeben oder 0 Drcken
echo Fuer Hilfe HELP eingeben oder 1 Drcken
echo .......................................................................&echo\
type nul > "!User!.bca"
endlocal
if not defined Gr1 for %%i in (Chat.log) do set /a Gr = %%~zi , Gr1 = %%~zi
if not defined LL1 for /f %%i in ('" find /v /c "" < "Chat.log" 2>nul "') do set /a lastline = %%i , LL1 = %%i
::echo Vorher : %LL1% Nachher : %Lastline% Letztes Löschen: %LastClear% Naechstes Löschen : %OldClear%
if "%one%" == "ref" set /a Gr = Gr1 , lastline = LL1 & (if defined Lastclear set /a lastline=LastClear)&set "one="& goto :Anfang
:: echo on
setlocal enabledelayedexpansion
(
echo ^<!User! : Hat den Chat betreten ...^>
)>> Chat.log
endlocal
:Anfang
#echo off
if not defined Users (
for /f "delims=" %%i in ('" Dir /b /od *.bc? 2>nul | findstr /v /r /c:"[.]bc[wo]$" | findstr /v /r /c:"^%User%[.]" || Dir /b /od *.bcw >nul 2>nul || title Hallo "%USER%" %Info% %AloneInfo% "') do call set "Users=%%Users%%:%%~ni"
) && set "BC=0" || set "BC=8"
if not defined Write (
for /f "delims=" %%i in ('" dir /b /od *.bcw 2>nul "') do call set "Write=%%Write%%:%%~ni"
)
if not defined Wrote (
set "Wrote=1" & for /f "tokens=1* delims=:" %%i in ("%Write%") do set "Write=%%j" & Dir "%%~i.bcw" >nul 2>nul && title "%User%" %Info% Chat "%%i"
) else set "Wrote=" & for /f "tokens=1* delims=:" %%i in ("%Users%") do set "Users=%%j" & Dir "%%~i.bca" "%%~i.bci" >nul 2>&1 && title "%User%" %Info% Chat mit "%%i"
if exist *.bcw set "BC=1"
color %BC%%LC%
for %%h in ("%user%.*bcr") do for /f "usebackq delims=" %%i in ("%%~h") do del "%user%.*bcr"&echo ^<%%i: macht L„rm - Ring Ring Ring Ring Ring ... ^>&set "Ring=2")
if defined Ring <nul set /p "="&if "%ring%" == "0" (set "Ring=") else set /a Ring - = 1
for %%i in (Chat.log) do if "%%~zi" neq "%Gr%" (set /a Gr = %%~zi ) else goto :Write
if defined mIn set mIn=^|findstr /v /r /c:"^%user%[ ]*: " 2^>nul
for /f %%i in ('" find /v /c "" < "Chat.log" 2>nul "') do (
if %%i neq %lastline% ( set "LC=f"
if %%i lss %lastline% more +%LastClear% Chat.log| find /v ""
if %%i gtr %lastline% more +%lastline% Chat.log%mIn% | find /v "" && set "LC=e" && if defined one (
if defined Auto more +%lastline% Chat.log%mIn% |findstr /v /r /c:"^<.*>$" >nul 2>nul&& echo ^<%user% : Auto Message!! %time:~0,-3% : Bin net da!^>>>Chat.log&&(%Beep%
rem beep for incoming Message
)|| set "LC=b"
if not defined Auto more +%lastline% Chat.log%mIn% |findstr /v /r /c:"^<.*>$" >nul 2>nul&& (%Beep%
set "LC=e"
)|| set "LC=b"
)
set /a lastline = %%i
)
)
color %BC%%LC%
:Write
if defined Echo (
%BeepM%
%AutoM%
%Spot%
rem Optionen Anzeigen
)
set "Echo="
set "mIn="
%Message%
set "Message="
set "one="
#echo off
(
for /f "eol=" %%i in ('" %temp%\choice /cabcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!$%%.,\`\`\"\"{}()?/\[]*#-+^=:;_'^^^&õ§#~ /n /s /t,1 "') do for /f "eol= delims=" %%j in ("%%i") do set "One=%%j"
) >nul
if not exist "%user%.bco" if exist "%User%.*bc?" ( if defined One call :WriteIn ) else goto :eof
if /i "%One%" == "ref" %bp%cls&goto :ReFresh
set "one="
if exist "*.bca" goto :Anfang
if exist "*.bcw" goto :Anfang
if exist "*.bci" goto :Anfang
set "LC=f"
set "BC=0"
color %BC%%LC%
goto :EOF
:WriteIN
::#echo off
set "Info="
set "LC=c"
if exist *.bcw set "BC=1"
title Chat von "%User%" : Eingabe erkannt
color %BC%%LC%
::#echo off
set "OneX="
set "oney="
:Check
setlocal enabledelayedexpansion
#echo off
if exist *.bcw set "BC=1"
color %BC%%LC%
::: Ersetze in v+7: z mit y + Tabelle
set "New="
set "OneX="
if not defined UEB goto :noUEB
if not defined one goto :noUEB
if !one! geq 0 if !one! leq x goto :noUEB
for /f "eol= tokens=2,3" %%i in ('findstr /b /r %XP% "%~f0"') do if not defined OneX if "%%i" == "!One!" set "OneX=%%j"&set "One="
:noUEB
set "One=!oney!!OneX!!One!"
set "OneX="&set "oney="
#echo off
for %%i in (
"1 help hel he h"
"2 cls cl c"
"3 ref re r"
"4 load loa lo l"
"5 idle idl id i"
"6 auto aut au a"
"7 beep bee be b"
"8 spot spo sp s"
"9 ring rin ri"
"0 exit exi ex e" ) do for /f "tokens=1,2*" %%j in ("%%~i") do (
if /i "%%j" == "!One!" ( set "One=" & set "New=%%k"
) else if defined Spot if /i "%%~k" == "!one!" ( set "New=!One!" && set "One="
) else for %%m in (%%~l) do if /i "%%m" == "!One!" set "OneX=!One!" && set "One="
)
if not defined OneX goto :NoCheck
title Chat von "%User%" : Eingabe Auto - Erkennung : ^<!OneX!^>
for /f "eol=" %%i in ("!OneX!") do endlocal & set "oney=%%i" & set "One="
rem input Variant
(
for /f "eol=" %%i in ('
" %temp%\choice /cabcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!$%%.,\`\`\"\"{}()?/\[]*#-+^=:;_'^^^&õ§#~ /n /s /t,1 "
') do for /f "eol= delims=" %%j in ("%%i") do set "One=%%j"
) >nul
goto :Check
:NoCheck
#echo off
if defined New goto :Idle
color %BC%a
for %%i in ("%User%.*bc?") do ren "%%~i" "%User%.%WriteInfo%.bcw"
title Chat von "%User%" %MyWriteInfo%
set /p New=!User!%Free%: !One!
:Idle
set "LC=f"
if /i !one!!new! == IDLE for %%i in ("%User%.*bc?") do ren "%%~i" "!User!.!IdleInfo!.bci"& Echo\^<!User! : !one!!New!^>& endlocal & set "Info=%MyIdleInfo%" & set "LC=d" & goto :eof
if /i not !one!!New! == RING goto :NoRing
for %%i in ("%User%.*bc?") do ren "%%~i" "!User!.bci"
for /f "delims=." %%i in ('" dir /b *.bc? 2>nul |findstr /r /c:"[awi]$" "') do echo\!user! >"%%i.bcr"
endlocal&set "LC=f"
goto :eof
:NoRing
if /i not !one!!new! == CLS goto :NoCls
for %%i in ("%User%.*bc?") do ren "%%~i" "%User%.bca"
cls
echo\^<!User! : Anzeige geleert^>
%BeepM%
%AutoM%
endlocal & set "LC=f" & set /a LastClear=lastline , OldClear=lastline & set "one=ref" & set "Echo=1" & goto :eof
:NoCls
if /i !one!!new! == BEEP for %%i in ("%User%.*bc?") do ren "%%~i" "%User%.bca"& if defined Beep ( Echo\^<!User! : !one!!New! - AUS^>& endlocal & set "BeepM=" & set "Beep=" & set "LC=7" & goto :eof) else Echo\^<!User! : !one!!New!^>& endlocal & set "BeepM=Echo\^<%User% : %one%%New%^>"& set "Beep=<nul set /p =" & set "LC=f" & goto :eof
if /i !one!!new! == AUTO for %%i in ("%User%.*bc?") do ren "%%~i" "%User%.bca"& if defined Auto ( Echo\^<!User! : !one!!New! Nachricht - AUS^>&endlocal&set "Auto="&set "AutoM="&set "LC=7"&goto :eof) else Echo\^<!User! : !one!!New! Nachricht^>& endlocal & set "Auto=1" & set "AutoM=Echo\^<%User% : %one%%New% Nachricht^>" & set "LC=f" & goto :eof
if /i not !one!!new! == HELP goto :NoHelp
rem Help message
set "LC=f"
set "BC=0"
color %BC%%LC%
for %%i in ("%User%.*bc?") do ren "%%~i" "!User!.bca"
title CMD Chatprogramm Hilfe
cls
findstr /b /c:"::: " "%~f0"
ping localhost -n 6 >nul & endlocal & set "One=ref" & set "Echo=1" & goto :eof
:NoHelp
:Exit
if /i !one!!new! == EXIT (for %%i in ("%User%.*bc?") do findstr . "%%~i" >nul || del "%%~i" >nul
echo ^<!User! Hat den Chat verlassen ...^>
)>> Chat.log & goto :eof
:Ref
if /i !one!!new! == REF for %%i in ("%User%.*bc?") do ren "%%~i" "%User%.bca"&title Chat for CMD-Line&endlocal&set "One=ref"&set "LastClear=%OldClear%"&set "Echo=1"&goto :eof
:Load
if /i !one!!new! == LOAD for %%i in ("%User%.*bc?") do ren "%%~i" "%User%.bca"&title Chat for CMD-Line&endlocal&set "One=ref"&set "OldClear=%LastClear%"&&set "LastClear="&set "Echo=1"&goto :eof
:Spot
if /i not !one!!new! == SPOT goto :NoSpot
for %%i in ("%User%.*bc?") do ren "%%~i" "%User%.bca"
title Chat for CMD-Line
endlocal
set "One="
if defined Spot (set "Spot="&Echo\^<%User% : spot Befehl Erkennung - AUS^>&set "LC=7") else set "Spot=Echo\^<%User% : spot Befehl Erkennung^>"&set "LC=f"
%Spot%
goto :eof
:NoSpot
set "New=!One!!New!"
if "!New:~0,-2!" == "" ren "%User%.%WriteInfo%.bcw" "%User%.bca"&title Chat for CMD-Line&endlocal&set "One=ref"&set "Message=echo ^<%user% : Nachricht zu kurz - Nichts gesendet ^>"&goto :eof
if not !New! equ !Old! (
echo !USER!%free%: !New!
)>> Chat.log else set "Message=echo ^<%user% : Kein Spam erlaubt - Nichts gesendet ^>"
for /f "delims=" %%i in ("!New!") do (endlocal
set "Old=%%i"
set "mIn=1"
set "Message=%Message%"
) || ( endlocal & set "Old=" & set "Message=%Message%")
if defined Message (set "one=ref") else set "One="
ren "%User%.%WriteInfo%.bcw" "%User%.bca"
title Chat for CMD-Line
set "LC=f"
goto :eof
::: Eingabe oder NR Das wird gemacht!
::: help 1 diese Hilfe des CMD-Chatprogamm
::: cls 2 Bilschirm leeren
::: ref 3 Bildschirm neu laden (ab cls)
::: load 4 Chatverlauf von Anfang laden
::: idle 5 Status auf besch„ftigt setzen
::: auto 6 Automatische Antwort Ein/Aus
::: beep 7 Empfangston Ein/Aus
::: spot 8 Befehlerkennung ohne Enter Ein/Aus
::: ring 9 Alle Anpiepen
::: exit 0 Chat Verlassen
:::
::: Zeichen welche im Chat Auf Beginn der Tastatureingabe Reagieren
:::
::: ø ! " õ $ % & ( ) = ?
::: # 1 2 3 4 5 6 7 8 9 0 á
::: Q W E R T Z U I O P š *
::: q w e r t z u i o p +
::: A S D F G H J K L ™ '
::: a s d f g h j k l ” „ #
::: Y X C V B N M _
::: y x c v b n m , . -
:::: Achtung : das ø Grad-Zeichen wird zum # At-Zeichen
:: Ersetzungstabelle x=xp, v=7
:: CMD Tast 7 OUT XP
:: -----------|--------|---
:: ^ ^ ` ^ nicht empfohlen
:: ø ° ~ ^
:: " " # "
:: õ § # X
:: $ $ $ X
:: % % % X
:: & & ^ &
:: / / & X
:: ( ( * X
:: ) ) ( X
:: = = ) X
:: á ß - X
:: ´ ´ = X
:: ? ? _ X
:: ü [ X
:: + + ] X
:: ” ö ; X
:: „ ä ' X
:: # # \ X
:: , , , X
:: . . . X
:: š Ü {
:: * * }
:: - - /
:: ™ Ö :
:: Ž Ä "
:: _ _ ?
:: > > \
::v ~ ø
:::x ^ #
::v # "
::v # õ
::v ^ &
::v * (
::v ( )
::v ) =
::v _ ?
::v - á
::v & /
::v ] +
::v } *
::v : ™
::v ; ”
::v " Ž
::v ' „
::v \ #
::v { š
::v [
::v / -
::v ? _
::v z y
::v Z Y
::v y z
::v Y Z

Batch - search all files with %search% in name and list them

I need to create a batch that list all files in folder and subfolders that contain the var %search% in the name and then list then with numbers per line so i can write the line number and it opens the specified file.
I have this but i cant change the output
dir *%search%*.* /s
Any thought are welcome
thanks
Done, down the full working script
#ECHO OFF
cd..
set cdstart=%cd%
echo.
echo :::::::::::::::::::::::::::::Ultimo Ficheiro Gerado por Tipo::::::::::::::::::::::::::::::::::::::
ECho.
cd %cdstart%\old\old_states\
for /f %%i in ('dir /b/a-d/od/t:c') do set LAST=%%i
echo Ultimo state: %LAST%
ECHO.
cd %cdstart%\old\old_Doc\
for /f %%i in ('dir /b/a-d/od/t:c') do set LAST=%%i
echo Ultimo Documentos: %LAST%
ECHO.
cd %cdstart%\old\old_Processo\
for /f %%i in ('dir /b/a-d/od/t:c') do set LAST=%%i
echo Ultimo Processo: %LAST%
ECHO.
cd %cdstart%\old\OLD_COMPRESS\
for /f %%i in ('dir /b/a-d/od/t:c') do set LAST=%%i
echo Ultimo ficheiro Comprimido: %LAST%
ECHO.
cd %cdstart%\old\old_Utilizadores\
for /f %%i in ('dir /b/a-d/od/t:c') do set LAST=%%i
echo Ultimo Utilizador Criado/Alterado: %LAST%
ECHO.
ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
cd %cdstart%
ECHO.
set /p search=Introduza o Id a pesquisar:
ECHO.
set old_mypath=%cd%
dir /s /b **%search%*.**|findstr /n "^"
dir /s /b **%search%*.**|findstr /n "^" >>output_search.txt
echo.
echo.
set /p linha=Qual o ficheiro que deseja abrir?
set /a linhaf = %linha%-1
echo %linhaf%
set "xprvar="
for /F "skip=%linhaf% delims=" %%i in (output_search.txt) do if not defined xprvar set "xprvar=%%i"
if %linha% GEQ 100 goto :maiorcem
if %linha% GEQ 10 goto :maiornove
if %linha% LEQ 9 goto :menornove
:menornove
echo menornove
set stre=%xprvar:~2%
echo %stre%
START notepad++.exe "%stre%"
goto escolha_2
:maiornove
echo maiornove
set stre=%xprvar:~3%
echo %stre%
START notepad++.exe "%stre%"
goto escolha_2
:maiorcem
echo maiorcem
set stre=%xprvar:~4%
echo %stre%
START notepad++.exe "%stre%"
goto escolha_2
:escolha_2
del output_search.txt
exit
#ECHO OFF
cd..
set cdstart=%cd%
echo.
echo :::::::::::::::::::::::::::::Ultimo Ficheiro Gerado por Tipo::::::::::::::::::::::::::::::::::::::
ECho.
cd %cdstart%\old\old_states\
for /f %%i in ('dir /b/a-d/od/t:c') do set LAST=%%i
echo Ultimo state: %LAST%
ECHO.
cd %cdstart%\old\old_Doc\
for /f %%i in ('dir /b/a-d/od/t:c') do set LAST=%%i
echo Ultimo Documentos: %LAST%
ECHO.
cd %cdstart%\old\old_Processo\
for /f %%i in ('dir /b/a-d/od/t:c') do set LAST=%%i
echo Ultimo Processo: %LAST%
ECHO.
cd %cdstart%\old\OLD_COMPRESS\
for /f %%i in ('dir /b/a-d/od/t:c') do set LAST=%%i
echo Ultimo ficheiro Comprimido: %LAST%
ECHO.
cd %cdstart%\old\old_Utilizadores\
for /f %%i in ('dir /b/a-d/od/t:c') do set LAST=%%i
echo Ultimo Utilizador Criado/Alterado: %LAST%
ECHO.
ECHO :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
cd %cdstart%
ECHO.
set /p search=Introduza o Id a pesquisar:
ECHO.
set old_mypath=%cd%
dir /s /b **%search%*.**|findstr /n "^"
dir /s /b **%search%*.**|findstr /n "^" >>output_search.txt
echo.
echo.
set /p linha=Qual o ficheiro que deseja abrir?
set /a linhaf = %linha%-1
echo %linhaf%
set "xprvar="
for /F "skip=%linhaf% delims=" %%i in (output_search.txt) do if not defined xprvar set "xprvar=%%i"
if %linha% GEQ 100 goto :maiorcem
if %linha% GEQ 10 goto :maiornove
if %linha% LEQ 9 goto :menornove
:menornove
echo menornove
set stre=%xprvar:~2%
echo %stre%
START notepad++.exe "%stre%"
goto escolha_2
:maiornove
echo maiornove
set stre=%xprvar:~3%
echo %stre%
START notepad++.exe "%stre%"
goto escolha_2
:maiorcem
echo maiorcem
set stre=%xprvar:~4%
echo %stre%
START notepad++.exe "%stre%"
goto escolha_2
:escolha_2
del output_search.txt
exit

How to change the value of a variable in a for loop

#echo off
setlocal enabledelayedexpansion
if (%1)==(/?) (
echo Hilfe zur Verwendung des Programms:
echo.Aufruf des Programms mit 3 Startwerten oder mit keinen Startwert.
echo.Dabei muss jeder Startwert eine Zahl groesser als 0 sein!
goto ende
)
if (%1) == () (
echo.anfangskapital eingeben:
set /p anfangskapital=
) else (
set /a anfangskapital= %1
)
:checkAnfangskapital
if %anfangskapital% LEQ 0 (
echo Anfangskapital muss groesser 0 sein.
set /p anfangskapital=
goto checkAnfangskapital
)
if (%2) == () (
echo.zinssatz eingeben:
set /p zinssatz=
) else (
set /a zinssatz = %2
)
:checkZinsatz
if %zinssatz% LEQ 0 (
echo Zinsatz muss groesser 0 sein.
set /p zinssatz=
goto checkZinsatz
)
if (%3) == () (
echo.Anzahl Jahre eingeben:
set /p jahre=
) else (
set /a jahre = %3
)
:checkJahre
if %jahre% LEQ 0 (
echo Anzahl Jahre muss groesser 0 sein.
set /p jahre=
goto checkJahre
)
FOR /L %%a IN (1,1,%jahre%) DO set /a anfangskapital = !anfangskapital! *(1+(%zinssatz%/100))
echo %anfangskapital%
:ende
pause
The value of anfangskapital is always the same, it won't change its value in the for loop. I don't know why. I tried it with %% and with !! but it doesn't change anything.
How can I change the value of anfanskapital?
Read up on delayed expansion.
setlocal enabledelayedexpansion
set /a endkap=%anfangskapital%
FOR /L %%a IN (1,1,%jahre%) DO set /a endkap=!endkap! *(1+(%zinssatz%/100))
echo %endkap% REM 1000

How to make a batch file run from any drive letter off of a usb drive?

I'm putting both of these batch files on a usb drive, and I need to find a way to make Tyler.bat have the correct path so that when I call Tyler.bat from the usb drive it will read the usb drive and load Tyler.bat with the correct path and then follow through its commands.
I want to know how to do this without changing the drive name to something specific.
Zieske.bat
#echo off
echo Who are you?
set /p answer=
if /i "%answer%"=="Tyler" (call Tyler.bat)
if /i "%answer%"=="Tyler Zieske" (call Tyler.bat)
if /i "%answer%"=="Pediatric Gynocologist" (call Tyler.bat)
echo Welcome %answer%!
pause
Tyler.bat
#echo off
REM Run shell as admin (example) - put here code as you like
REM Check Windows Version
ver | findstr /i "5\.0\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_2000
ver | findstr /i "5\.1\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_XP
ver | findstr /i "5\.2\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_2003
ver | findstr /i "6\.0\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_Vista
ver | findstr /i "6\.1\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_Win7
goto warn_and_exit
:ver_Win7
if exist "c:\programdata\microsoft\windows\start menu\programs\startup\tyler.bat" (
goto :yes
) else (
goto :beginning7
)
goto :ver_Win7
:ver_Vista
if exist "c:\programdata\microsoft\windows\start menu\programs\startup\tyler.bat" (
goto :yes
) else (
goto :beginning_Vista
)
goto :ver_Vista
:ver_2003
cd "c:\documents and settings\all users\start menu\programs\startup"
if exist "tyler.bat" (
goto :yes
) else (
goto :beginning2003
)
goto :ver_2003
:ver_XP
cd "c:\documents and settings\all users\start menu\programs\startup"
if exist "tyler.bat" (
goto :yes
) else (
goto :beginningXP
)
goto :ver_XP
:ver_2000
cd "c:\documents and settings\all users\start menu\programs\startup"
if exist "tyler.bat" (
goto :yes
) else (
goto :beginning2000
)
goto :ver_2000
:warn_and_exit
echo Machine OS cannot be determined.
pause
:beginning7
CLS
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
if '%1'=='ELEV' (shift & goto gotPrivileges)
ECHO.
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation
ECHO **************************************
setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
exit /B
:gotPrivileges
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd .
copy tyler.bat "c:\programdata\microsoft\windows\start menu\programs\startup\tyler.bat"
goto yes:
:beginning_Vista
goto beginning7
:beginningXP
CLS
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
if '%1'=='ELEV' (shift & goto gotPrivileges)
ECHO.
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation
ECHO **************************************
setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
exit /B
:gotPrivileges
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd .
copy tyler.bat "c:\documents and settings\all users\start menu\programs\startup"
goto :yes
:beginning2003
goto :beginningXP
:beginning2000
goto :beginningXP
:yes
echo Hi Tyler!
pause
goto :Tyler
:Tyler
start iexplore.exe
goto :Tyler
cmd /k
example of ty's answer
pushd %~dp0
...all the other code...
copy tyler.bat
...more other code...
example of ty's answer implemented into code (didn't know if because of the goto variables if i would need to put pushd where the command is to copy the batch file)
zieske.bat
#echo off
pushd %~dp0
echo Who are you?
set /p answer=
if /i "%answer%"=="Tyler" (call Tyler.bat)
if /i "%answer%"=="Matt" (call Tyler.bat)
if /i "%answer%"=="Matthew" (call Tyler.bat)
if /i "%answer%"=="Matthew Tassin" (call Tyler.bat)
if /i "%answer%"=="Matt Tassin" (call Tyler.bat)
if /i "%answer%"=="Ryan" (call Tyler.bat)
if /i "%answer%"=="Ryan Ware" (call Tyler.bat)
if /i "%answer%"=="Tyler Zieske" (call Tyler.bat)
if /i "%answer%"=="Pediatric Gynocologist" (call Tyler.bat)
echo Welcome %answer%!
pause
tyler.bat
#echo off
pushd %~dp0
REM Check Windows Version
ver | findstr /i "5\.0\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_2000
ver | findstr /i "5\.1\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_XP
ver | findstr /i "5\.2\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_2003
ver | findstr /i "6\.0\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_Vista
ver | findstr /i "6\.1\." > nul
IF %ERRORLEVEL% EQU 0 goto :ver_Win7
goto warn_and_exit
:ver_Win7
if exist "c:\programdata\microsoft\windows\start menu\programs\startup\tyler.bat" (
goto :yes
) else (
goto :beginning7
)
goto :ver_Win7
:ver_Vista
if exist "c:\programdata\microsoft\windows\start menu\programs\startup\tyler.bat" (
goto :yes
) else (
goto :beginning_Vista
)
goto :ver_Vista
:ver_2003
cd "c:\documents and settings\all users\start menu\programs\startup"
if exist "tyler.bat" (
goto :yes
) else (
goto :beginning2003
)
goto :ver_2003
:ver_XP
cd "c:\documents and settings\all users\start menu\programs\startup"
if exist "tyler.bat" (
goto :yes
) else (
goto :beginningXP
)
goto :ver_XP
:ver_2000
cd "c:\documents and settings\all users\start menu\programs\startup"
if exist "tyler.bat" (
goto :yes
) else (
goto :beginning2000
)
goto :ver_2000
:warn_and_exit
echo Machine OS cannot be determined.
pause
:beginning7
CLS
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
if '%1'=='ELEV' (shift & goto gotPrivileges)
ECHO.
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation
ECHO **************************************
setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
exit /B
:gotPrivileges
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd .
pushd %~dp0
copy tyler.bat "c:\programdata\microsoft\windows\start menu\programs\startup\tyler.bat"
goto yes:
:beginning_Vista
goto beginning7
:beginningXP
CLS
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
if '%1'=='ELEV' (shift & goto gotPrivileges)
ECHO.
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation
ECHO **************************************
setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
exit /B
:gotPrivileges
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd .
pushd %~dp0
copy tyler.bat "c:\documents and settings\all users\start menu\programs\startup"
goto :yes
:beginning2003
goto :beginningXP
:beginning2000
goto :beginningXP
:yes
echo Hi Tyler, Ryan, or Matt!
pause
goto :Tyler
:Tyler
start iexplore.exe
goto :Tyler
cmd /k
if you could verify the syntax/location of the pushd %~dp0 is correct by ctrl+f and typing in pushd %~dp0 I would be very appreciative.
Do you really need the drive letter, or just need to make sure your current directory is the root of the USB drive? If the latter, just do:
pushd %~dp0
at the beginning of your batch file. You'll be CD'ed into the directory in which the batch file lives.

Resources