Problems using the operator "+=" in batch file - command-line

Ok
It's simple. I just want to add 1 to a number every time trought the use of += operator!
So i go in prompt just like this:
C:\Users\fsilveira>SET teste=000007
C:\Users\fsilveira>ECHO %teste%
000007
C:\Users\fsilveira>SET /A teste+=1
8
C:\Users\fsilveira>
Wow nice. Seems to be working just fine.
From the behaviour of the last one, if I use the same operator again, it should just add one to eight right? So I guess I will have 9? But here is what's happening:
C:\Users\fsilveira>SET teste=000008
C:\Users\fsilveira>ECHO %teste%
000008
C:\Users\fsilveira>SET /A teste+=1
1
C:\Users\fsilveira>
What? 8 + 1 is 1 ? o_O
When it comes to the number 8 it does not work how it should (or how I believe it's suppose to)
I'm going insane over here.
Please some one could help me and explain to me what's happening?
I really dont know!
Regards,
Filipe

When prefixing with 0 it is intrepeated as an octal number. And 00008 is not a valid octal number. You can see the effect of this by the following:
C:\Users>SET teste=000020
C:\Users>ECHO %teste%
000020
C:\Users>SET /A teste+=1
17
where 00020 in octal is 16 in decimal.

The number 8 doesn't have 5 leading zeros. If you're doing math, use real numbers. :-)
This works fine on my machine in a command window in Win7 64-bit:
C:\Users\Ken>set /a teste=8
8
C:\Users\Ken>set /a teste+=1
9
C:\Users\Ken>set /a teste+=1
10
C:\Users\Ken>echo %test3%
10
C:\Users\Ken>

You can avoid this by removing leadig zeros:
C:\>set teste=000008
C:\>echo %teste%
000008
C:\>for /f "tokens=1*delims=0" %i in ("$0%teste%") do #set teste=%j
C:\>set /a teste+=1
9

The leading zeros makes things complicated; I would have expected the '000007' not to have worked in that way - the bottom line is: the '000008'; or any '8' with leading zeros is being handled as a string. Example:
C:\Users\op>set f=foo
C:\Users\op>echo %f%
foo
C:\Users\op>set /a f+=1
1

In response to
"number 7 doesn't have 5 leading zeros too.. but still works with the operator!"
You will only have trouble with the leading zeros for numbers > 7 because 0-7 Octal are the same as 0-7 Decimal!

Related

How to create 100 files on windows folder with rdfc.exe?

I am trying to create 100 random files (each of 1 MB) . Found that rdfc.exe can be used for this purpose. I can create a single file with command
c:\rdfc>rdfc.exe z:\test1.txt 1 MB
Random Data File Creator v0.1.0.4 (2004/12/19)
Copyright (C)2004 by Michael Berthold
Visit http://www.bertel.de/software/rdfc
1.00 MB written to 'z:\test1.txt' in 0 sec. (ca. 0.00 MB/sec.)
However I need to create 100 files using this? Can anyone help with a powershell script of batch file to do this?
CW answer of Squashmans comment:
Using a for /l loop you can perform a loop n times by using the syntax
for /l %%g in (1,1,n) do <yourCommand>
So in your case this would evaluate to
for /l %%g in (1,1,100) do rdfc.exe Z:\test%%g.txt 1 MB
%%g contains the current number in this case between 1 and 100. For command-line use only, you have to use only one percentage signs in both places.
Note that you probably can prevent these messages by prepending the line #echo off to your script in case you are executing above with it.
try Something like this
$Expath="PAth_Of_rdfc/rdfc.exe "
1..100 | %{. $Expath $("z:\test{0}.txt" -f $_) 1 MB}

Script to batch rename to retain only some characters of original filename?

I need to rename thousands of rar files with original filenames of variable sizes. I must make them 10 characters long by keeping the first 3 and the last 4 characters of the original filename and adding in the middle 3 random characters [numbers].
Example:
input:
"John Doe - Jane Doe - 19073275.rar"
"XXXX - XYXY- 98705674.rar
output:
"Joh1273275.rar"
"XXX9795674.rar"
Next, the .bat should generate a .txt with the original name and the modified one underneath for each file!
I know it's possible but I'm completely stupid when it comes to writing it. Please help!
The Batch file below do what you want:
#echo off
setlocal EnableDelayedExpansion
for %%a in (*.rar) do (
set name=%%~Na
set num=00!random!
set newName=!name:~0,3!!num:~-3!!name:~-4!
ren "%%a" "!newName!%%~Xa"
echo "%%a" modified to "!newName!%%~Xa" >> log.txt
)
I'd write a script to generate the names in any simple way (say first 6 + last 4), and then check for any duplicates to be cleaned up by hand (or a second pass shifting the middle, or ...). Unless this is a repetitive job (do it daily), it isn't worth fully automatizing.

How does °, ± & ² produce ░, ▒ & ▓ in this batch file?

This code:
#echo off
set /p a=Installing [<nul
set b=1
:loop
if %b% leq 2 set /p a=°<nul
if %b% gtr 4 if %b% leq 6 set /p a=±<nul
if %b% gtr 6 if %b% leq 8 set /p a=²<nul
if %b% gtr 8 if %b% leq 10 set /p a=±<nul
if %b% gtr 10 set /p a=°<nul
choice /t 1 /c y /d y>nul
set /a b=%b%+1
if %b%==13 (echo ]&goto :eof)
goto :loop
... came from http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1237-DOS-ECHO-text-to-previous-line-by-Paul-Tomasi.html#c18182 and produces the below output in a cmd window:
Installing [░░▒▒▓▓▒▒░░]
However, I cant seem to find an explanation through google as to how this works. Substituting other extended ascii characters into their place seems to produce "The syntax of the command is incorrect", so have only managed to guess that this is a hack whereby something exchanges these characters to lower characters in the extended ascii set, by way of unintended-by-the-designers trick.
If anyone could also suggest a good website for learning more about cmd / cmd programs' features like this, it would be appreciated.
It's because your editor uses a different encoding from the one DOS and the command line use.
See characters 176-178 here:
http://academic.evergreen.edu/projects/biophysics/technotes/program/ascii_ext-pc.htm
Character pairs in columns DOS and WIN are represented by the same numerical values, but DOS (and hence the command line) and Windows (your text editor) display these numbers as different symbols.
In case it's not clear: your text file is a series of bytes (numbers) and one of these bytes has value 176. Under DOS code page 437 it used to denote ▓ glyph, and command line, for historical reasons, uses the same encoding as DOS did. But your text editor, running on Windows, apparently reads the file using the old Windows-1252 encoding, where 176 means °.
You could try to find an editor supporting 437, it would save you from such confusions.

cmd line rename file with date and time

Project moving forwards, I can see why creating .bat files to do things can become addictive!
I can now save somefile.txt at regular intervals, I then rename somefile.txt by adding the time and date to create a unique file name
ren somefile.txt somefile_%time:~0,2%%time:~3,2%-%date:~-10,2%%date:~3,2%%date:~-4,4%.txt
As an example, the code above has just renamed somefile.txt to somefile_1317_13022011.txt (1317hrs on 13th February 2011)
I ran
ren somefile.txt somefile_%time:~0,2%%time:~3,2%-%date:~-10,2%%date:~7,2%%date:~-4,4%.txt
yesterday, it ran successfully until midnight, and then it crashed (syntax error) although it was saving as 12012011 for the date (12th Jan 2011) instead of the correct date of 12022011.
Will the current version ran past midnight? Am I confusing myself with UK vs US date format?
Animuson gives a decent way to do it, but no help on understanding it. I kept looking and came across a forum thread with this commands:
Echo Off
IF Not EXIST n:\dbfs\doekasp.txt GOTO DoNothing
copy n:\dbfs\doekasp.txt n:\history\doekasp.txt
Rem rename command is done twice (2) to allow for 1 or 2 digit hour,
Rem If before 10am (1digit) hour Rename starting at location (0) for (2) chars,
Rem will error out, as location (0) will have a space
Rem and space is invalid character for file name,
Rem so second remame will be used.
Rem
Rem if equal 10am or later (2 digit hour) then first remame will work and second will not
Rem as doekasp.txt will not be found (remamed)
ren n:\history\doekasp.txt doekasp-%date:~4,2%-%date:~7,2%-%date:~10,4%_#_%time:~0,2%h%time:~3,2%m%time:~6,2%s%.txt
ren n:\history\doekasp.txt doekasp-%date:~4,2%-%date:~7,2%-%date:~10,4%_#_%time:~1,1%h%time:~3,2%m%time:~6,2%s%.txt
I always name year first YYYYMMDD, but wanted to add time. Here you will see that he has given a reason why 0,2 will not work and 1,1 will, because (space) is an invalid character. This opened my eyes to the issue. Also, by default you're in 24hr mode.
I ended up with:
ren Logs.txt Logs-%date:~10,4%%date:~7,2%%date:~4,2%_%time:~0,2%%time:~3,2%.txt
ren Logs.txt Logs-%date:~10,4%%date:~7,2%%date:~4,2%_%time:~1,1%%time:~3,2%.txt
Output:
Logs-20121707_1019
Digging up the old thread because all solutions have missed the simplest fix...
It is failing because the substitution of the time variable results in a space in the filename, meaning it treats the last part of the filename as a parameter into the command.
The simplest solution is to just surround the desired filename in quotes "filename".
Then you can have any date pattern you want (with the exception of those illegal characters such as /,\,...)
I would suggest reverse date order YYYYMMDD-HHMM:
ren "somefile.txt" "somefile-%date:~10,4%%date:~7,2%%date:~4,2%-%time:~0,2%%time:~3,2%.txt"
following should be your right solution
ren somefile.txt somefile_%time:~0,2%%time:~3,2%-%DATE:/=%.txt
I took the above but had to add one more piece because it was putting a space after the hour which gave a syntax error with the rename command.
I used:
set HR=%time:~0,2%
set HR=%Hr: =0%
set HR=%HR: =%
rename c:\ops\logs\copyinvoices.log copyinvoices_results_%date:~10,4%-%date:~4,2%-%date:~7,2%_%HR%%time:~3,2%.log
This gave me my format I needed:
copyinvoices_results_2013-09-13_0845.log
problem in %time:~0,2% can't set to 24 hrs format, ended with space(1-9), instead of 0(1-9)
go around with:
set HR=%time:~0,2%
set HR=%Hr: =0% (replace space with 0 if any <has a space in between : =0>)
then replace %time:~0,2% with %HR%
good luck
ls | xargs -I % mv % %_`date +%d%b%Y`
One line is enough.
ls all files/dirs under current dir and append date to each file.
I tried to do the same:
<fileName>.<ext> --> <fileName>_<date>_<time>.<ext>
I found that :
rename 's/(\w+)(\.\w+)/$1'$(date +"%Y%m%d_%H%M%S)'$2/' *

Need assistance padding numerical month and day with leading 0

I am working within a batch file and need to pad a single digit with a leading 0 if under 10. I have the values in environmental variables. They are month and day, I need to pad to match file structure I am working against. I am using vbscript to return a date that comes back in the following format "7/16/2009". Need it to look like "07/16/2009" and most inportantly need each item in separate EVs.
VBscript:
WScript.Echo DateAdd("d", Date, -36)
Batch:
for /F "tokens=1-3 delims=/" %%x in ('cscript //nologo get36thday.vbs') do (
SET YYYY=%%z
SET MM=%%x
SET DD=%%y)
VBScript:
dteOldDate = Now()
strNewDate = Right("00" & Month(dteOldDate), 2) & "/" & Right("00" & Day(dteOldDate), 2) & "/" & Year(dteOldDate)
For the batch script, I don't know the exact syntax but a batch script can return a specified number of characters from the right side of a string.
So, append the month after a "0" character and take the 2 right-most digits. It would probably look something similar to this:
SET MM=0%%x
SET MM=%MM:~-2%
1 become 01
5 becomes 05
10 stays 10
Here is my method of padding strings.
In this example, MYVAR will be padded to five zeroes.
SET MYVAR=00000%MYVAR%
SET MYVAR=%MYVAR:~-5%
Athough the other answers are useful if you don't know the length of the string you want to pad with zeros up front, for dates and times a simple string replacement will do.
set hour=!TIME:~0,2!
set hour=!hour: =0!
In short, if there is a space it will be replaced by a 0.