This is much easier than what I'm making it...
I have a non-delimited input file that I want to grab three columns from and output them to a new layout. The input file is fixed length. I want to take the data from the following positions:
field1 - position 1 for 7 characters
field2 - position 13 for 50 characters
field3 - position 187 for 10 characters
I want my output file to be field1,field2,field3...comma delimited.
This is what I thought would work, but alas, it doesn't:
#echo off
setlocal EnableDelayedExpansion
for /F "tokens=*" %%A in (C:\mydata.txt) do (
set line=%%A
set var_id=!line:~0,7%!
set var_name=!line:~12,50%!
set var_amt=!line:~186,10%!
set ABC=!%var_id%%var_name%%var_amt%!
echo ABC
)
I need a script which rename all files in all sub folders of script's root.
I did search and found something which works and I can modify (I am a little bit newbie/laic)
#echo off
chcp 65001
setlocal enabledelayedexpansion
set filename=image
set /a x=1
>#rename.txt (
for /r %CD% %%f in (*.jpg) do (
echo rename "%%f" "!filename!_!x!.jpg"
rename "%%f" "!filename!_!x!.jpg"
set /a x+=1
)
)
endlocal
pause
But I want this to rename files to random string.
I found a lots of scripts which can generate random strings, but I just can't make them to work inside the FOR brackets.
for now im stuck with
#echo off
chcp 65001
setlocal enabledelayedexpansion
set /a x=%RANDOM%/99
>#rename.txt (
for /r %CD% %%f in (*.jpg) do (
echo "%%f" renamed to "!x!.jpg"
rename "%%f" "!x!.jpg"
set /a x+=%RANDOM%/99
)
)
endlocal
Which works fine, but has his limitations.
Any help will be appreciated
#echo off
chcp 65001
setlocal enableextensions enabledelayedexpansion
set "alphabet=a b c d f g h i j k l m n p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9"
set "size=0"
for %%a in (%alphabet%) do (
set "a.!size!=%%a"
set /a "size+=1"
)
for /r %CD% %%f in (*.jpg) do (
set "k="
for /l %%a in (1 1 64) do (
set /a "r=!random! %% size"
for %%b in (!r!) do set "k=!k!!a.%%b!"
)
rename "%%f" "!k!.jpg"
)
endlocal
iam trying to put if loops in batch script but not getting desired result, please look into code ans suggest where iam wrong in if loop
IF %banned% == 1 echo "vvvvv" it not comparing properly
even 1==1 and 1==0 is same for batch script?
call E:\utility\batfiles\DBAEnvProd.cmd
set dbname=UMRdb
set proc=UMRdb.[dbo].[maintenancemode]
call %osqlExeLocation% -E -d%dbname% -S%svr% -w%w% -b -Q"exec %proc%" -o%pathout%%Maintmode.txt
#echo on
set "usrname=Y"
set "banfile=E:\utility\sysout\Maintmode.txt"
find /i "%usrname%" "%banfile%" >nul 2>&1&&set /a banned=1 || set /a banned=0
echo %banned%
echo comparing maintenance
IF %banned% == 1 echo "vvvvv"
(
here banned is 1 then i dont want to execute rest of code it shd
go to endgood
)
else
normal code should be executed
#echo on
REM ********************************************************************************
REM JOB: TCMP_DBA_M_MNT_UMR_ESSENTIAL
REM
REM ********************************************************************************
set job=TCMP_DBA_M_MNT_UMR_ESSENTIAL
set RetCode=0
time /T & date /T
erase %pathout%%job%*.suc > nul
erase %patherr%%job%*.err > nul
REM ********************************************************************************
REM to display date and time
for /F "tokens=1-3 delims=:. " %%A in ('time/T') do set var=%%A%%B%%C%
set timestamp=%date:~4,2%%date:~7,2%%date:~10,4%
set timestamp1=%timestamp%%var%
REM
REM ********************************************************************************
:main
echo --- DB Growth Info
set proc=UMRdb.[dbo].[spCollectDBSpaceInfo]
call %osqlExeLocation% -E -d%dbname% -S%svr% -w%w% -b -Q"exec %proc%" -o%pathout%%job%_spCollectDBSpaceInfo.txt
if errorlevel 1 (
set RetCode=1
goto endbad
)
echo --- Cleanup of output files older than 30 days in sysout folder
call forfiles -p E:\utility\sysout /D -30 /M *.txt /C "cmd /c del #file"
:ENDGOOD
set RetCode=0
ECHO SUCCESS!!! %svr% %job% %proc%
echo %ErrMsg% > %pathout%%job%.SUC
GOTO END
:ENDBAD
set RetCode=1
ECHO ERROR!!! %svr% %job% %proc%
echo SEE %pathout%%job%_%proc%.txt FOR ERRORS > %patherr%%job%.ERR
:END
time /T & date /T
echo %RetCode%
%pathexe%cc.exe %RetCode%
REM EXIT
this is problematic
IF %banned% == 1 echo "vvvvv"
if == performs a string comparision and i'm seeing lots of spaces. if %banned% is 1 here you are comparing " 1" == "1 ". As you set/a banned=1, it would be better do a numeric comparision
IF %banned% EQU 1
or quoted string comparision
If "%banned%" == "1"
I'm working on a small batch script, which includes a section similar to the code block below... it would be an understatement to say this has befuddled me to the point where my mind has gone completely numb... why on gaias green backside does this not work...?
#echo off
set log=0
choice /m "Choose "
if errorlevel 2 set log=N
if errorlevel 1 set log=Y
echo %log%
pause
if "%log%"=="N" (
echo hello
)
if "%log%"=="Y" (
echo goodbye
)
pause
if errorlevel 2 set log=N
if errorlevel 1 set log=Y
translated:
if errorlevel is 2 or greater than 2 set log=N
if errorlevel is 1 or greater than 1 set log=Y
so - reverse the lines since if errorlevel is 2, it is both 2 or greater than 2 (so set N) and then is 1 or greater than 1 (so set Y)
An oddity within Windows (and old DOS) is that if you set "if errorlevel ..." it actually means "if the errorlevel is greater than this number..." So if you say "if errorlevel 1" you mean "if errorlevel > 1".
Try this:
if errorlevel 1 if not errorlevel 2 (do stuff)
if errorlevel 2 if not errorlevel 3 (do other stuff)
Alternatively, you can use the temporary variable %ERRORLEVEL%...
I want to modify this script. I can't get it to accept wildcard charcters:
#echo off
setLocal EnableDelayedExpansion
set /a value=0
set /a sum=0
FOR /R %1 %%I IN (*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
#echo Size is: !sum! k
It is in a batch file called dirsize and is called like so:
dirsize c:\folder
I want it to check the folder sizes for me. This one here is an example, the cache in firefox:
dirsize C:\users\%username%\AppData\Local\Mozilla\Firefox\*.default\Cache
It returns the value 0.
But if I go
dirsize C:\users\%username%\AppData\Local\Mozilla\Firefox\sr1znnb4.default\Cache
It works and I get the value 55322 returned.
PowerShell makes this easy, of course:
(gci . -Recurse | Measure-Object -Property Length -Sum).Sum
And PowerShell is already installed on Windows 7. Get on the bandwagon! :-)
Also, C:\users\%USERNAME%\AppData\ isn't a reliable way of finding AppData. Try %APPDATA% (or $env:APPDATA in PowerShell).
Ok for a starter, lets say wildcards are not well supported throughout batch files, and specially in for loops.
That's what I came up with:
DirSize.cmd "valid path"
#echo off
rem Args[1]= "Path" or "valid path with wildcard"
set _size=0
set dRaw=%~1
echo %~1|findstr "*"&& set dSource=%~dp1|| (
set dSource=%~1\
if not "%dRaw:~-1%"=="\" set dRaw=%dRaw%\
)
for /f "delims=" %%D in ('dir /ad /b %dRaw%') do call :DirSize "%dSource%%%~D"
set /a size=_size/1024
echo size=%size% k (%_size% bytes)
exit /b 0
:DirSize
call :FileSize "%~1"
for /f "delims=" %%d in ('dir /ad /b "%~1"2^>nul') do call :DirSize "%~1\%%~d"
exit /b 0
:FileSize
for /f "delims=" %%f in ('dir /a-d /b "%~1\"2^>nul') do for %%F in (%1\%%f) do set /a _size+=%%~zF
exit /b 0
___Usage___
Argument: Needs to be a path (not a file) that either contain wildcards or not. Path need top be valid when typing dir path
___Notes___
This script does check for all files and sub-directories, even hidden/system ones.
change the dir /a argument and add -h-s (like /ad-h-s) to remove this behaviour.
I revised the argument checking to my liking now, so I think it is usable.
I hope this helps.
You would need to wrap what you currently have in another for loop which expands the * into a series of directories.
So
#echo off
setLocal EnableDelayedExpansion
set /a sum=0
for /d %%D in (%1) do (
FOR /R %%D %%I IN (*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
)
echo Size is: !sum! k
might work (untested, though, but should give you the general idea)
If you have a Windows version of grep (you can find one at GNU utilities for Win32) you can do something like this:
dir /s %1 | grep --after-context=1 "Total Files Listed:"
This code will increase directory size limit up to 2,000,000 TB and will display more real size, what would be useful if the folder contains too many files.
rem SET DIR=%1
SET DIR=C:\DIR1\
IF NOT EXIST "%DIR%" (
ECHO Directory "%DIR%" does not exist.
goto END
)
setlocal EnableDelayedExpansion
set GB=
set SIZE=
set exp=000000000
FOR /R "%DIR%" %%F IN (*) DO (
set /a SIZE=!SIZE!+%%~zF
if !SIZE! geq 1!exp! (
set /a GB=!GB!+!SIZE:~0,-9!
for /f "tokens=* delims=0" %%a in ("!SIZE:~-9!") do set SIZE=%%a
)
)
if not defined SIZE set SIZE=0
if not defined GB goto EOS
FOR /L %%N IN (1,1,9) DO (
if "!SIZE:~%%N!" == "" (
set SIZE=!GB!!exp:~%%N!!SIZE!
goto EOS
)
)
:EOS
call :Divide !SIZE! 1024 SIZEKB
echo Size is: !SIZE!B (!SIZEKB!kB)
endlocal
:END
pause
exit
:Divide
set Num1=%~1
set Num2=%~2
set %3=
set Num=
set Number=
IF !Num2! EQU 0 goto :EOF
FOR /L %%N IN (0,1,18) DO (
if "!Num1:~%%N!" == "" goto :EOD
set Number=!Number!!Num1:~%%N,1!
if !Number! geq !Num2! (
set /a quotient=!Number!/!Num2!
set /a Number=!Number!-!quotient!*!Num2!
if !Number! equ 0 set Number=
set Num=!Num!!quotient!
) else (
if defined Num set Num=!Num!0
)
)
:EOD
if not defined Num set Num=0
set %3=%Num%
goto :EOF
10x faster than doing it through FOR.
With the advantage of being able to use any of the permitted parameters of the DIR command (in this case /S)
#ECHO OFF
SET folder="C:\WINDOWS"
call:timer
ECHO Processing, please wait...
CALL :folderSize size %folder% "/S"
CALL :strNumDivide sizeKb %size% 1024
CALL :strNumDivide sizeMb %sizeKb% 1024
CALL :strNumDivide sizeGb %sizeMb% 1024
CALL :formatNumber size %size%
CALL :formatNumber sizeKb %sizeKb%
CALL :formatNumber sizeMb %sizeMb%
CALL :formatNumber sizeGb %sizeGb%
ECHO.
ECHO Size of %folder%:
ECHO.
ECHO %size% bytes
ECHO %sizeKb% Kb
ECHO %sizeMb% Mb
ECHO %sizeGb% Gb
ECHO.
pause
exit /b
:: Function to calculate the size of a directory and its subdirectories
::----------------------------------------------------------------------
:folderSize <returnVariableName> <folder> [DIR parameters]
SetLocal EnableExtensions EnableDelayedExpansion
SET folder=%2
SET params=%~3
IF NOT DEFINED folder SET folder="%CD%"
DIR %params% /W "%folder:"=%" > %TEMP%\folderSize.tmp
FOR /F "tokens=1 delims=:" %%x IN ('findstr /n /e ":" %TEMP%\folderSize.tmp') DO (SET line=%%x)
IF DEFINED line (
SET /A line = !line! + 1
FOR /F "tokens=4 delims= " %%i IN ('findstr /n "bytes" %TEMP%\folderSize.tmp^|findstr "!line!:"') DO (SET size=%%i)
SET size=!size:.=!
) ELSE (
FOR /F "tokens=3 delims= " %%i IN ('findstr /e "bytes" %TEMP%\folderSize.tmp') DO (SET size=%%i)
SET size=!size:.=!
)
DEL %TEMP%\folderSize.tmp > nul
EndLocal & SET "%~1=%size%"
GOTO:EOF
:: Extras functions to convert between different units and to give numerical format
:: --------------------------------------------------------------------------------
:strNumDivide <returnVariableName> <stringNum> <divisor>
SetLocal EnableExtensions EnableDelayedExpansion
SET strNum=%~2
SET divisor=%~3
SET result=
SET number=
IF !divisor! EQU 0 GOTO:EOF
FOR /L %%n IN (0,1,18) DO (
IF NOT "!strNum:~%%n!" == "" (
SET number=!number!!strNum:~%%n,1!
IF !number! GEQ !divisor! (
SET /A quotient=!number! / !divisor!
SET /A number=!number! - !quotient! * !divisor!
IF !number! EQU 0 SET number=
SET result=!result!!quotient!
) ELSE (
IF DEFINED result SET result=!result!0
)
)
)
IF NOT DEFINED result SET "result=0"
EndLocal & SET "%~1=%result%"
GOTO:EOF
:formatNumber <returnVariableName> <number> [separator [group size]]
SetLocal EnableExtensions EnableDelayedExpansion
SET "raw=%~2"
SET "separator=%~3"
SET "group=%~4"
SET "answer="
IF NOT DEFINED raw GOTO :EOF
IF NOT DEFINED separator SET "separator=."
IF NOT DEFINED group SET "group=3"
FOR %%g IN (-%group%) DO (
FOR /F "tokens=1,2 delims=,." %%a IN ("%raw%") DO (
SET int=%%a
SET frac=%%b
FOR /F "delims=:" %%c IN ('^(ECHO;!int!^& Echo.NEXT LINE^)^|FindStr /O "NEXT LINE"') DO (
SET /A length=%%c-3
)
FOR %%c IN (!length!) DO (
SET radix=!raw:~%%c,1!
)
FOR /L %%i IN (!length!, %%g, 1) DO (
SET answer=!int:~%%g!!separator!!answer!
SET int=!int:~0,%%g!
)
)
)
SET answer=%answer: =%
SET answer=%answer:~0,-1%
EndLocal & SET "%~1=%answer%%radix%%frac%"
Goto :EOF
Result output example:
(in the example, the routine took 3 seconds to calculate the size of the C: \ WINDOWS with a size of 28GB)
Processing, please wait...
Size of "C:\WINDOWS":
30.936.389.769 bytes
30.211.318 Kb
29.503 Mb
28 Gb
Elapsed Time: 3 secs