Windows batch script to substitute tabs with 2 spaces [closed] - windows

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need to substitute tab with 2 spaces in all files on a specified path using windows batch script. I see that it is not easy to enter tab character correctly into a batch file. Can anyone help. Thanks

EDIT Corrected output of leading spaces.
The tricky part is to grab the tab char. May be...
#echo off
setlocal EnableDelayedExpansion
rem Grab TAB character
rem http://www.dostips.com/forum/viewtopic.php?f=3&t=7898
set "TAB="
rem First, try the method for Windows XP
for /F "skip=4 delims=pR tokens=2" %%a in ('reg query hkcu\environment /v temp' ) do set "TAB=%%a"
rem Then, the method for newer versions
rem http://www.dostips.com/forum/viewtopic.php?f=3&t=1733&p=6840#p6853
for /F "tokens=2 delims=0" %%a in ('shutdown /? ^| findstr /BC:E') do if not defined TAB set "TAB=%%a"
rem set desired number of spaces
set "SPACES= "
pushd "your_desired_path"
for /f "delims=" %%f in ('dir /B *.txt') do (
> "output_%%~nxf" (
for /f "tokens=* delims=" %%i in (%%~nxf) do (
set "myData=%%i"
set "myData=!myData:%TAB%=%SPACES%!"
echo !myData!
)
)
echo copy /y "output_%%~nxf" "%%~nxf"
echo del "output_%%~nxf" >NUL 2>NUL
)
popd
endlocal
exit/B
Remove echo from copy and del lines above, if it seems to do the work.

Related

How to use separate Windows batch file with using FOR (tokens, delims)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I would like to make a performance monitoring batch program. I myself made insert VB. But the thing is, I can't make a file which is for insert
dump performance monitoring file
process the file into insert-able data
send to a server via FTP
insert the data
Except 2nd I made it all
would anybody help how to separate the data to what I want to make?
The dump file is like this
------------------------------------------------
**# 20180917_CAFENOIR_PERF.cvs**
------------------------------------------------
"(PDH-CSV 4.0) (","\\CAFENOIR\Processor(_Total)\% Processor time","\\CAFENOIR\Memory\Available KBytes","\\CAFENOIR\LogicalDisk(C:)\% Free Space","\\CAFENOIR\LogicalDisk(D:)\% Free Space","\\CAFENOIR\LogicalDisk(E:)\% Free Space"
"09/17/2018 12:32:11.439"," ","2389340","73.031078258802481","99.758165860552879","92.077869960114995"
"09/17/2018 12:32:12.474","13.17071949707611","2393976","73.031078258802481","99.758165860552879","92.077869960114995"
and I want to make the file like this
------------------------------------------------
**# 20180917_12_CAFENOIR_perfmon.imp**
------------------------------------------------
CAFENOIR
"13.17071949707611"
"2393976"
"73.031078258802481"
"99.758165860552879"
"92.077869960114995"
"09/17/2018 12:32:12.474"
I tried with a script like this but it just outputs the computername
CAFENOIR
I hope anyone gives a clue
added Code (Sorry I forgot)
for /F "tokens=1-6 skip=2 delims=," %%a IN ('type *.cvs') do (#echo %computername% %%b% %%c% %%d% %%e% %%f% %%a% > %date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%computername%_perfmon.imp
)
Looking at your answer, there are way to many issues with it. You could have run a single loop, and there is no such thing as %%a%:
#echo off
for /F "tokens=1-6 skip=2 delims=," %%a IN ('type *.cvs') do (
set "vDT=%%a"
set "vCPU=%%b"
set "vMEM=%%c"
set "vCfree=%%d"
set "vDfree=%%e"
set "vEfree=%%f"
echo %computername% %vCPU% %vMEM% %vCfree% %vDfree% %vEfree% %vDT% > %date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%computername%_perfmon.imp
)
Even better, you do not need to even set the variables as you predefine them already:
#echo off
for /F "tokens=1-6 skip=2 delims=," %%a IN ('type *.cvs') do (
echo %computername% %%b %%c %%d %%e %%f %%a > %date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%computername%_perfmon.imp
)
It's funny. I was struggling for a week for this, but after I posted the question I just did it myself
I hope it's gonna help someone who what separate A file parameters.
#SET vCPU = none
#SET vMEM = none
#SET vCfree = none
#SET vDfree = none
#SET vEfree = none
#SET vDT = none
FOR /F "tokens=1 skip=2 delims=," %%a IN ('type *.cvs') DO #SET vDT=%%a%
FOR /F "tokens=2 skip=2 delims=," %%a IN ('type *.cvs') DO #SET vCPU=%%a%
FOR /F "tokens=3 skip=2 delims=," %%a IN ('type *.cvs') DO #SET vMEM=%%a%
FOR /F "tokens=4 skip=2 delims=," %%a IN ('type *.cvs') DO #SET vCfree=%%a%
FOR /F "tokens=5 skip=2 delims=," %%a IN ('type *.cvs') DO #SET vDfree=%%a%
FOR /F "tokens=6 skip=2 delims=," %%a IN ('type *.cvs') DO #SET vEfree=%%a%
#echo %vCPU%
#echo %vMEM%
#echo %vCfree%
#echo %vDfree%
#echo %vEfree%
#echo %vDT%
#echo %computername% %vCPU% %vMEM% %vCfree% %vDfree% %vEfree% %vDT% > %date:~0,4%%date:~5,2%%date:~8,2%_%time:~0,2%_%computername%_perfmon.imp**

Variable value not showing in Win Batch [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am writing a batch to automate the installation of a a number of settings, INI files and APKs on a multitude of devices. I am getting errors in the batch due to the variables in showing NULL.
I have checked that the config.ini and ADBCommands.ini and adb+.bat are in the correct location.
Can you help?
::Global
#echo off
cd C:\
::INI Locations
set mypath=%~dp0
set config=%mypath%config.ini
set Commands=%mypath%ADBCommands.ini
set multi=%mypath%adb+.bat
#pause
::Set Varialbes
#for /f "tokens=1,2 delims==" %%a in (%config%) do (
if %%a==Build set Build=%%b
if %%a==Version set Version=%%b
if %%a==Creator set Creator=%%b
if %%a==DateModified set DateModified=%%b
if %%a==ScreenTimeout set ScreenTimeout=%%b
if %%a==UnknownSources set UnknownSources=%%b
if %%a==ScreenBrightness set ScreenBrightness=%%b
)
#for /f "tokens=1,2 delims==" %%d in (%Commands%) do (
if %%d==ScreenTimeoutCommand set ScreenTimeoutCommand=%%e
if %%d==UnknownSourcesCommand set UnknownSourcesCommand=%%e
if %%d==ScreenBrightnessCommand set ScreenBrightnessCommand=%%e
if %%d==OpenSOTICommand set OpenSOTICommand=%%e
)
::Build information
cls
#echo.
#echo.
#echo BUILD: %Build%
#echo VERSION: %Version%
#echo BUILD CREATOR: %Creator%
#echo LAST UPDATED: %DateModified%
#echo.
#echo.
#pause
Contents the ini files
config.ini
Build=XCover 3
Version=1.0.0
Creator=James B
DateModified=20/02/2017
ScreenTimeout=300000
UnknownSources=1
ScreenBrightness=225
Contents the ini files
ADBCommands.ini
ScreenTimeoutCommand=shell settings put system screen_off_timeout
UnknownSourcesCommand=shell settings put system install_non_market_apps
OpenSOTICommand=shell am start -n net.soti.mobicontrol.elm.samsung/net.soti.mobicontrol.startup.SplashActivity
Because your path has spaces you need to enclose the path with quotes. But when you do this, you need to tell the FOR /F command that you are still parsing a file and not a variable. So you need to use the USEBACKQ option.
#for /f "usebackq tokens=1,2 delims==" %%a in ("%config%") do (
AND
#for /f "usebackq tokens=1,2 delims==" %%d in ("%Commands%") do (

How to swap odd and even lines in a text file? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a text file. I have to swap odd and even lines.
I made a batch script that writes even lines into testfile2.txt and odd lines into testfile3.txt.
#echo off
setlocal EnableDelayedExpansion
set "filepath1=C:\\Users\\andyb\\Desktop\\testfile.txt"
set "filepath2=C:\\Users\\andyb\\Desktop\\testfile2.txt"
set "filepath3=C:\\Users\\andyb\\Desktop\\testfile3.txt"
set counter=0
set B=0
for /F %%A in (%filepath1%) do (
set /a B=!counter!%%2
if !B! equ 0 (echo %%A>>%filepath2%) else (echo %%A>>%filepath3%)
set /A counter=counter+1
)
And I want to take 1 line from file that contains odd lines, then 1 line from the file with even lines and write it to my first file. But I don't understand how to do it in FOR loop because it reads a line from only one file and I can't work with another file in this loop.
Example of input file:
1a
2b
3c
4d
Example of output file:
2b
1a
4d
3c
Try the following:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
rem // Define constants here:
set "_FILE=textfile.txt"
rem // Count number of lines:
for /F %%C in ('^< "!_FILE!" find /C /V ""') do set "COUNT=%%C"
rem // Divide by two, round up:
set /A "COUNT=(COUNT+1)/2"
< "!_FILE!" > "!_FILE!.tmp" (
rem // Read files in blocks of two lines:
for /L %%I in (1,1,%COUNT%) do (
set "LINE1=" & set "LINE2="
set /P LINE1=""
set /P LINE2=""
echo(!LINE2!
echo(!LINE1!
)
)
rem // Overwrite original file:
> nul move /Y "!_FILE!.tmp" "!_FILE!"
endlocal
exit /B
There are several solutions for this task.
The first one uses delayed expansion on execution of all lines of batch file exchanging odd and even lines. This means it does not work right for lines with an exclamation in line because ! is removed from line.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "SourceFile=%USERPROFILE%\Desktop\TestFile.txt"
if not exist "%SourceFile%" goto EndBatch
set "TargetFile=%USERPROFILE%\Desktop\TestFile2.txt"
del "%TargetFile%" 2>nul
set "LineOdd="
for /F "usebackq delims=" %%I in ("%SourceFile%") do (
if not defined LineOdd (
set "LineOdd=%%I"
) else (
echo %%I>>"%TargetFile%"
echo !LineOdd!>>"%TargetFile%"
set "LineOdd="
)
)
if defined LineOdd echo !LineOdd!>>"%TargetFile%"
move /Y "%TargetFile%" "%SourceFile%"
:EndBatch
endlocal
Blank and empty lines are skipped by FOR and therefore missing in target file. And lines starting with a semicolon ; are ignored on reading each line by FOR and for that reason are missing also in output file. But those limitations should not matter here according to input example.
The limitations of first solution could be avoided using this batch code which is of course much slower:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SourceFile=%USERPROFILE%\Desktop\TestFile.txt"
if not exist "%SourceFile%" goto EndBatch
set "TargetFile=%USERPROFILE%\Desktop\TestFile2.txt"
del "%TargetFile%" 2>nul
set "LineOdd="
for /F "tokens=1* delims=:" %%H in ('%SystemRoot%\System32\findstr.exe /N /R "^" "%SourceFile%"') do (
if not defined LineOdd (
set "LineOdd=_%%I"
) else (
if "%%I" == "" (
echo/>>"%TargetFile%"
) else (
echo %%I>>"%TargetFile%"
)
setlocal EnableDelayedExpansion
if "!LineOdd!" == "_" (
echo/>>"%TargetFile%"
) else (
echo !LineOdd:~1!>>"%TargetFile%"
)
endlocal
set "LineOdd="
)
)
if defined LineOdd (
setlocal EnableDelayedExpansion
if "!LineOdd!" == "_" (
echo/>>"%TargetFile%"
) else (
echo !LineOdd:~1!>>"%TargetFile%"
)
endlocal
)
move /Y "%TargetFile%" "%SourceFile%"
:EndBatch
endlocal
It would be also possible to use hybrid batch file JREPL.BAT written by Dave Benham:
call jrepl.bat "^(.*)\r\n(.*)\r\n" "$2\r\n$1\r\n" /M /X /F "%USERPROFILE%\Desktop\TestFile.txt" /O "%USERPROFILE%\Desktop\TestFile2.txt"
move /Y "%USERPROFILE%\Desktop\TestFile2.txt" "%USERPROFILE%\Desktop\TestFile.txt"
The last line of the file must have a DOS/Windows line termination (carriage return \r and line-feed \n) if being an even line on using this solution.
For understanding the used commands/executables/batch files and how they work, open a command prompt window, execute there the following command lines, and read entirely all help pages displayed for each command/executable/batch file very carefully.
del /?
echo /?
endlocal /?
findstr.exe /?
for /?
goto /?
if /?
jrepl.bat /?
move /?
set /?
setlocal /?
Read also the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul and >>.
The following example will create two files from testfile.txt, file0.out containing the even lines, and file1.out containing the odd lines.
#Echo Off
SetLocal EnableDelayedExpansion
For /F "Tokens=1* Delims=:" %%A In ('FindStr/N "^" "testfile.txt"') Do (
Set/A "_=%%A%%2"
(Echo(%%B)>>file!_!.out)
Rename the output files according to your requirements.
I think to reinterleave the odd and even versions in reversed order isn't that difficult. Appending to Compo's try:
#Echo Off
SetLocal EnableDelayedExpansion
Set File=testfile
For /F "Tokens=1* Delims=:" %%A In ('FindStr/N "^" "%File%.txt"'
) Do Set/A "_=%%A%%2"&>>%File%_!_!.txt Echo(%%B
<%File%_0.txt (For /f "delims=" %%A in (%File%_1.txt) Do (
Set "B="&Set /P "B="
Echo(!B!
Echo(%%A
)) >%File%-new.txt
Del %File%_*
In case of an uneven total the second last line will be empty. sample Output:
2b
1a
4d
3c
5e

How to compare file names in directory with file names in a text file?

I try to improve an overview of my search and find memories for batch files in Windows XP command prompt environment.
In order to my previous sentence I am not happy with my search possibilities and have to post a question.
I try to compare the names of some text files and have written words in a text file that are by reading the same. With such a start environment I wrote following batch script to get an echo output.
The aim is
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%b in ('dir "C:\A Folder"') do set var=%%~nb & echo !var!
rem The output is the name of the files without extension. Now my question:
rem Is it possible to compare the above file names with some input
rem from a text file, for example like:
for /f "delims=" %%b in ('dir "C:\A Folder"') do set var=%%~nb & for /f %%a in (Textfile.txt) do (if !var!==%%a echo good else echo search)
rem That returns no output. I would like to know if there are possibilities
rem to do that? And if it is possible, how to revise this batch file?
endlocal disabledelayedexpansion
pause
Have a nice day, wishes
Stefan
This should work with Latin characters - some foreign characters may not work:
#echo off
for /f "delims=" %%b in ('dir /b /a-d "C:\A Folder\*.*" ') do find /i "%%~nb" < "textfile.txt" >nul && (echo "%%~nb" found) || (echo "%%~nb" not found)
pause
proper formatting you code increases readabilty:
for /f "delims=" %%b in ('dir /b /a-d "C:\A Folder"') do (
for /f %%a in (textfile.txt) do (
if "%%~nb"=="%%a" ( echo good ) else ( echo search )
)
)
I added a /b to the dircommand (show name only, no date/time/attributes) and a /a-d to exclude directorynames.
You don't need to use a variable (!var!) here (but you can, it works fine).

Delete files in a directory that do not match certain file extensions [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am trying to write a simple batch file that deletes all files in a directory and its containing sub folders that DON'T match some file extensions.
Logic is like this:
GOTO directory
delete !(.avi, .mkv, .mp4)
Thought this would be simple enough but I have tried multiple times with no luck!
#echo off
for /r C:\test %%a in (*) do (
if not %%~xa==.avi (
if not %%~xa==.mkv (
if not %%~xa==.mp4 (
del /f /q "%%a"
)
)
)
)
Just repeat the if's for the extensions that you don't want to delete.
This answer will work for a few extensions, but if you have any more you would find a better answer here.
This question is nearly identical to Delete *.* excluding some extensions, except you want to delete from the sub-directories as well. Either of the following adaptations of the top two answers should work.
Both of the following assume your batch script is not in the folder hierarchy that is getting deleted.
#echo off
for /f %%F in ('dir /s /b /a-d "yourDirPath\*"^| findstr /vile ".avi, .mkv, .mp4"') do del "%%F"
.
#echo off
setlocal EnableDelayedExpansion
set "exclude=.avi.mkv.mp4."
for /r "yourDirPath" %%F in (*) do if /I "%exclude%" == "!exclude:%%~xF.=!" del "%%F"
If your script is in the hierarchy, then an extra IF statement will fix the problem.
#echo off
for /f %%F in ('dir /s /b /a-d "yourDirPath\*"^| findstr /vile ".avi, .mkv, .mp4"') do if "%%~fF" neq "%~f0" del "%%F"
.
#echo off
setlocal EnableDelayedExpansion
set "exclude=.avi.mkv.mp4."
for /r "yourDirPath" %%F in (*) do if /I "%exclude%" == "!exclude:%%~xF.=!" if "%%~fF" neq "%~f0" del "%%F"
Similar to this question: How can I delete files that don't match a wildcard?
FOR /R [directory] %%F IN (*.*) DO IF NOT "%%~xF" == ".[extension]" DEL /F /S "%%F"

Resources