Syntax of the "for" command: whats wrong with line 10 in following batch file? - date

1 echo off
2
3 echo Please enter a date
4 set /p a=
5 echo %a%
6 for /D %%d in (M:\Serienbriefauftrag\*) do (
7 if not exist %%d\Erledigt\*.bat echo %%d
8 )
9 pause
10 for /d %%b in (dir M:\Serienbriefauftrag\%%d /T:C)
11 echo %%b
12 pause
The Code works fine until line 10 (syntax error) the aim is, to get the creation date of the folder compare it with "%a%" and and if it's under the entered date "%a%", the folder should be moved. but somehow...
found a way to get the creation date
if not exist %%d\Erledigt\*.bat echo %%~td|findstr /i /l
the new task is to make the output "calculateable"

Two things:
1) In line 10 %%d is undefined (empty), since the scope of the first loop (where it is defined) is left in line 8.
You can set another variable to %%d to be able to use it after the loop ends, but be careful. In batch using set in for loops is a bit tricky. To get around the aweful use of delayed variable expansion, I would suggest to you staying inside of the loop while doing your work.
#echo off
set /p a=Please enter a date:
for /d %%d in (M:\Serienbriefauftrag\*) do (
if not exist %%d\Erledigt\*.bat (
:: Do whatever you want with %%d in here
echo "%%d"
)
)
pause
2) The syntax of your second loop is wrong. You are missing the do before the loop body and /d lets the loop iterate over all folders in a specified directory. dir M:\Serienbriefauftrag\%%d /T:C is not a directory but a command.
If you want to iterate over the output of that command, you have to use /f and put the contents of the paranthesis in single quotes, like that ('dir M:\Serienbriefauftrag\%%d /T:C').

Related

Get last line of a URL and save as integer variable in batch file

I want to get last line of this link (https://pastebin.com/raw/s5BxuEEw) and +1 it and save as integer.
For example if the last line is 5 , put 6 in variable.
I can get content with this code but I dont know how to filter last line:
#echo off
for /f "tokens=*" %%i in ('powershell /command "(Invoke-WebRequest -Uri 'https://pastebin.com/raw/s5BxuEEw').Content"') do set return=%%i
echo "%return%"
pause
To select only the last line from url content use index [-1]
(but the for /f would nevertheless iterate ALL lines and only the last persists)
To add / increment a number use set /A
#echo off
set "URI=https://pastebin.com/raw/s5BxuEEw"
for /f "tokens=*" %%i in ('
powershell -NoP -C "(Invoke-WebRequest -Uri '%URI%').Content[-1]"
') do set /A "return=%%i+1"
echo "%return%"
pause
Sample output is
"6"

Batch Script to check first 5 char vs MM-DD

I'm trying to build a script for one of our remote media players and am trying to get it to update once a file shows up in the dropbox. I need it to check the first 5 of the title against MM-DD and if they match then play the video in question. Playing the video is no issue, neither are the archives. My issue right now is that when I try to make a for loop for the files in the location I'm getting the syntax of the command is incorrect or "x" was not expected at this time. also, my date is being formatted like this: 05 -02, and I dont know why.
:::::::::::::::::::::::::::::::::::::::::::::: Get Date :::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Use WMIC to retrieve date and time
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
IF "%%~L"=="" goto s_done
Set _mm=00%%J
Set _dd=00%%G
)
:s_done
:: Pad digits with leading zeros
Set _mm=%_mm:~-2%
Set _dd=%_dd:~-2%
::Finalize Date
Set date=%_mm%-%_dd%
echo %date%
::::::::::::::::::::::::::: Check the downloads folder for completed video files :::::::::::::::::::::::::::::::
:: Loop through all files
:loop
for %%a IN ("dir \Dropbox\Trailer") DO (
::IF the name is not blank, get the first 5 characters of the video name
set first5=%a:~0,5%
::Compare those characters to the date
IF "%first5%" == "%date%" (
taskkill /im wmplayer.exe /t
::::::::::::::: Archive all previous Videos :::::::::::::
for /r %%i in ("dir \Dropbox\Trailer") do (
xcopy /s (%%i) "dir \Archived_Shows\"
)
ping localhost
"C:\Program Files\Windows Media Player\wmplayer.exe" "C:\Dropbox\Trailer\%%a" /fullscreen
:::::::::::::::::::::::::::::::: Exit if new video is running ::::::::::::::::::::::::::::::::::::::
exit
)
)
goto :loop
I'm not sure exactly what your script is supposed to be doing but here is an example based on my understanding.
#Echo Off
CD "Dropbox\Trailer" 2>Nul || Exit /B
For /F "Tokens=1-2" %%A In (
'WMIC Path Win32_LocalTime Get Day^,Month^|FindStr [1-9]'
) Do Set /A _dd=10%%A,_MM=10%%B
Set "DString=%_MM:~-2%-%_dd:~-2%"
:Loop
If Not Exist "%DString%*" (
Timeout 300 /NoBreak
GoTo Loop
)
TaskKill /IM WMPlayer.exe /T 2>Nul
Timeout 10 /NoBreak >Nul
XCopy "." "%~dp0Archived_Shows" /S /D /I
For %%A In ("%DString%*") Do (
"%ProgramFiles%\Windows Media Player\wmplayer.exe" "%%A" /FullScreen
)
GoTo Loop
If no matching file is found it currently checks for new ones every 300 seconds, (5 minutes). If a matching file is found, the loop only resumes once you close WMPlayer. You can change that timespan on line 11 as per your preference.
You are setting the variable %date%, which is a system dynamic variable. Attempting to modify the contents is an unrecommended act, so instead, use another name.
Also, I have simplified the get-and-trim MM-DD part, and it works properly for me.
for /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
IF "%%~L"=="" goto s_done
Set _mm=00%%~J
Set _dd=00%%~G
)
:s_done
::Finalize Date
Set titleDate=%_mm:~-2%-%_dd:~-2%
echo %titleDate%
pause

batch file to open txt replace text save and close

I understand that this community is for helping people and I'm not one hundred percent how to ask for someone to do a commissioned job or w/e but I'd like a series of batch files for changing certain numbers to a particular number per batch file.
echo off
if exist %application.yml del %application.yml
for /F "delims=" %%l in (%1) do (
set "line=%%l"
set "line=%line:specific text=another word%"
echo/%line%>> %application.yml)'
How would I set this to replace an array of numbers to the set number, I'm sorry I'm a complete idiot with this.
File itself application.yml
product:
# Cntrl alt 1 Mini, 2 m16, 3 scar, 4 m4169, 5 akm, 6 ump,7 sks
discordUsername:
discordPassword:
maxCPUsUsage: 4
shutdownKey: 0x7B
reloadConfigKey: 0x79
screenResolutionX: 1920
screenResolutionY: 1080
playerEsp: true
playerEspColor: 0xFF00FF
playerEspPointSize: 1
radarEsp: true
radarEspColor: 0x0000FF
radarEspPointSize: 2
vehicleEsp: true
vehicleEspColor: 0xff0000
vehicleEspPointSize: 1
vehicleEspHotkey: 0x2D
lootEsp: true
lootEspColor: 0x00FF00
lootAirdropEspColor: 0xffffff
lootEspPointSize: 1
lootEspHotkey: 0x24
redrawTime: 0
reloadDataTime: 2000
aimEnabled: true
aimKey: 0x39
aimFOV: 95
aimFrequency: 6
aimPrecision: 1
aimSpeed: 1
bulletSpeed: 990
aimHitbox: 0
# Key list: https://msdn.microsoft.com/en-us/en-en/library/windows/desktop/dd375731(v=vs.85).aspx
So essentially the number I wish to replace is the bullet speed and it could be multiple numbers from previous changes ect, for example
990
870
400
and if it is any of these instances I wish to replace it with
900
and so on and so forth.
So it searches for any of the possible numbers and changes it to the specified number and save the file under the same file name.
Thanks in advance for any help if I'm being completely honest in the example I dont even know what to change to make it simply replace one number to the other never mind multiple instance searches :)
Once again ty for any advice or help.
The following commented .bat script should do the job, although empty lines are not copied to output file:
#ECHO OFF
SETLOCAL EnableExtensions
rem redirect all output to a brand new file
>application.yml (
rem parse a file (supplied file name %1 must not be equal to "application.yml")
for /F "usebackq delims=" %%l in ("%~1") do (
rem parse each line
for /F "tokens=1* delims=:" %%L in ("%%~l") do (
if /I "%%~L"=="bulletSpeed" (
rem modified line
echo(%%L: 900
) else (
rem original line
echo(%%l
)
)
)
)
BTW, in the original script, you would need Delayed Expansion to make proper reference to variables (re)defined within a parenthesised code block (like a FOR loop body), e.g. as follows:
echo off
SETLOCAL EnableExtensions EnableDelayedExpansion
if exist application.yml del application.yml
for /F "delims=" %%l in (%1) do (
set "line=%%l"
set "line=!line:specific text=another word!"
echo/!line!>>application.yml
)
Resources (required reading):
(command reference) An A-Z Index of the Windows CMD command line
(helpful particularities) Windows CMD Shell Command Line Syntax
(%~L etc. special page) Command Line arguments (Parameters)
(special page) EnableDelayedExpansion
(>>, > etc. special page) Redirection
Regular Expressions are ideal to do this sort of editing.
Aside from findstr's limited capabilities batch is lacking RE support.
This wrapped powershell one liner will do the job either on cmd line or in a batch-file.
powershell -NoP -C "(gc application.yml) -Replace '(?<=bulletSpeed: )\d+','900'|sc application.yml"
It uses a lookbehind assertion to replace any number following bulletSpeed: with 900.
To limit the replacement to one of previous 990, 870, 400 use this:
powershell -NoP -C "(gc application.yml) -Replace '(?<=bulletSpeed: )(990|870|400)','900'|sc application.yml"

Batch file check file get updated to today's date(System Date)

I want to create a batch to check if the file have been modified to today's date, what i did was to "bring in a system's date and compare it with the modified date, if they match, then trigger something. My batch file works well and displays two right dates, but the IF statement saying the date mismatch.
#ECHO OFF
for /f "tokens=1,2,3,4 delims=. " %%i in ('date /t') do set date=%%k%%j
echo %date%
pause
FOR %%a IN (D:\MyFile.txt) DO SET FileDate=%%~ta
set DATEONLY=%FileDate:~0,10%
echo %DATEONLY%
pause
if DATEONLY==date (
echo date ok
)
else (
cls
ECHO Wrong
)
PAUSE
There are the following problems:
do not use variable name date as this is a built-in variable containing the current date (type set /? for help);
the first for statement is useless, because %date% is already available;
the strings DATEONLY and date are compared literally in your if statement, you need to state %DATEONLY%==%date% instead;
the else statement must be in the same line as the closing parenthesis of the if body (type if /? for help);
So try this:
#ECHO OFF
echo %date%
pause
FOR %%a IN (D:\MyFile.txt) DO SET FileDate=%%~ta
set DATEONLY=%FileDate:~0,10%
echo %DATEONLY%
pause
if %DATEONLY%==%date% (
echo date ok
) else (
ECHO Wrong
)
PAUSE
Note: Regard that all those dates in the batch file are locale-dependent.
Here is a completely different approach:
forfiles /P . /M MyFile.txt /D +0 /C "cmd /C echo #fdate #file"
The forfiles command is capable of checking the file date. In the above command line, it:
walks through the current directory (.),
lists all files named MyFile.txt (of course there is onlyone),
but only if it has been modified +0 days after today,
and then executed the command line after the /C switch.
If MyFile.txt has been modified today (or even in future), the given command line is executed;
if it has been modified earlier than today, an error message is displayed and ERRORLEVEL is set to 1.
Notice that forfiles is not a built-in command and might not be available on your operating system.

Batch file : copy all file except those its name contain some substring

first of all im beginner. i want to create batch file to search through specific folder (including all it subfolder) and copy all file inside it except those which filename contain some specific string,this is what i have so far
set now=fish
set logDirectory="C:\Users\paiseha\Desktop\bb\"
for /r %logDirectory% %%i IN (*%now%*.*) do (
rem copy process goes here
)
let say i have 3 file in it
C:\Users\fareast\Desktop\bb\one.txt
C:\Users\fareast\Desktop\bb\twofishtwo.txt
C:\Users\fareast\Desktop\bb\three.txt
so i want to copy file one.txt and three.txt only, but instead it copy only the second one,i know its because of *%now%*.* so how can i invert it so that it does the other way around, help me pls, thanks in advance
try:
#ECHO OFF &setlocal
set "now=fish"
set "logDirectory=C:\Users\paiseha\Desktop\bb"
for /f "delims=" %%a in ('dir /a-d/b/s "%logDirectory%"^|findstr /riv "^.*[\\][^\\]*%now%[^\\]*$"') do (
rem copy process goes here
)
EDIT: The \ character is represented as [\\] instead of \\ because of a quirk on how Vista FINDSTR regex escapes \. Vista requires \\\\, but XP and Win 7 use \\. The only representation that works on all platforms is [\\]. See What are the undocumented features and limitations of the Windows FINDSTR command? for more info.
for /f "delims=" %%a in ('dir /a-d/s/b "%logDirectory%" ') do echo %%~nxa|findstr /i /L "%now%" >nul&if errorlevel 1 ECHO COPY "%%a"
should work for you.