format command looping - command-line

If I do the below in a batch files of "format.cmd" and run it - 9 times out of 10 it fails. The prompt keeps on looping the command again and again..? Any idea how to fix this?
Command I run:
echo y | format D: /V:DATA /Q
Output: It puts more spaces after the echo y then the command above? I am thinking that is the problem but I have no idea what it is.
C:\Temp\1>echo y | format D: /V:DATA /Q
C:\Temp\1>echo y | format D: /V:DATA /Q
C:\Temp\1>echo y | format D: /V:DATA /Q
C:\Temp\1>echo y | format D: /V:DATA /Q
C:\Temp\1>echo y | format D: /V:DATA /Q
C:\Temp\1>echo y | format D: /V:DATA /Q
C:\Temp\1>echo y | format D: /V:DATA /Q
C:\Temp\1>echo y | format D: /V:DATA /Q
C:\Temp\1>echo y | format D: /V:DATA /Q
C:\Temp\1>echo y | format D: /V:DATA /Q
C:\Temp\1>echo y | format D: /V:DATA /Q
continues until I have to kill it.
Thanks,

You are trying to execute the format.com file from the format.cmd batch file. However, the order of precedence for like-named files is .cmd first, then .com, so what is happening is that format.cmd is running itself and not your format.com.
Try making format.cmd look like this to explicitly call the .com version.
echo y|format.com D: /V:DATA /Q

Related

How to execute a powershell file in batch repeatadly

I have a question. With my batch program I want to call a powershell program to replace words. I tried it with batch, but in my files are very often exclamation marks and yeah...batch don`t like these. So I take my batch strings and put them into powershell. This works perfectly fine. But I want to repeat this with other words and there is my problem.
set /a loop=0
:inta
set /a loop=%loop%+1
call:DoReplace "%Torsten%" "%Torsten:~0,-1%%loop%" cache\%KlausDieter:~0,-5%%loop%%Rainer% cache1\%KlausDieter:~0,-5%%loop%%Rainer%
exit /b
:DoReplace
echo ^(Get-Content "%3"^) ^| ForEach-Object { $_ -replace %1, %2 } ^| Set-Content %4>Rep.ps1
Powershell.exe -executionpolicy remotesigned -File Rep.ps1
if exist Rep.ps1 del Rep.ps1
GOTO :wer
:wer
set installel=%errorlevel%
if %loop%==%count% goto fall
if %installel%==0 goto inta
:fall
set /a loop=0
:intal
set /a loop=%loop%+1
call:DoReplaceL "%Horst%" "%Horst:~0,-1%%loop%" cache0\%Bruno:~0,-5%%loop%%Rainer% cache1\%Bruno:~0,-5%%loop%%Rainer%
exit /b
:DoReplaceL
echo ^(Get-Content "%3"^) ^| ForEach-Object { $_ -replace %1, %2 } ^| Set-Content %4>Rep.ps1
Powershell.exe -executionpolicy remotesigned -File Rep.ps1
if exist Rep.ps1 del Rep.ps1
GOTO :werl
:werl
set installel=%errorlevel%
if %loop%==%count% goto falll
if %installel%==0 goto intal
:falll
When I call it a second time there come a error with: Argument "Rep.ps1" isnt available. Isnt it possible to do such things?
I am from Germany so sorry for the bad english.
Thank you for your help
Markus
My batch-code for search and replace was this. Maybe you will find a mistake to get rid of problems with exclamation marks:
set /a loop=0
:inta
set /a loop=%loop%+1
SET "quell_datei=cache\%KlausDieter:~0,-5%%loop%.cin"
SET "ziel_datei=cache1\%KlausDieter:~0,-5%%loop%.cin"
SET "suchen_nach=%Torsten%1"
SET "ersetzen_durch=%Torsten%%loop%"
IF NOT DEFINED suchen_nach (ECHO. Fehler: Die Variable suchen_nach nicht definiert^^!&GOTO :eof)
del "%ziel_datei%" >nul 2>&1
FOR /f "tokens=1,* delims=:" %%a IN ('type "%quell_datei%" ^| FINDSTR /n "^"') DO (
CALL :ers "%%b"
)
GOTO :wer
:ers
set "zeile=%~1"
if defined zeile set "zeile=!zeile:%suchen_nach%=%ersetzen_durch%!"
>>"%ziel_datei%" echo.!zeile!
GOTO :eof
:wer
set installel=%errorlevel%
if %loop%==%count% goto fall
if %installel%==0 goto inta
:fall
echo.

Is there a way to get a subtracted date from Powershell in a batch file?

I'm trying to create a .bat file get the current business day-1, so if it's Monday the date should be the Friday date(current date -3), if it's Friday the date should be Thursday date(current date -1). I can get the dates from Powershell but when i try to get the date with the subtraction it gives me this error:
get-date was unexpected at this time.
This is generated in the last part of the powershell command in bold text:
if %WEEKDAY%==3 (for /f %%i in ('powershell $dataAtual=get-date;$dataAtual=$dataAtual.AddDays(-3); **get-date** $dataAtual') do set FX=%%i) else (set FX=%CURRENTDATE%)
echo %FX%
I tried to remove the get-date so it just prints the value of $dataAtual like in powershell but it them says:
$dataAtual was unexpected at this time
Full code:
for /f %%i in ('powershell get-date -f yyy.MM.dd') do set CURRENTDATE=%%i
echo %CURRENTDATE%
for /f %%i in ('powershell get-date %CURRENTDATE% -UFormat %%u') do set WEEKDAY=%%i
echo %WEEKDAY%
if %WEEKDAY%==3 (for /f %%i in ('powershell $dataAtual=get-date;$dataAtual=$dataAtual.AddDays(-3); get-date $dataAtual') do set FX=%%i) else (set FX=%CURRENTDATE%)
echo %FX%
(I know weekday 3 is not Friday in the code above, I'm just testing for now)
Refer to the Theo's Comment , You can write it into a batch file like this :
#echo off
set psCmd="&{$dataAtual=(Get-Date).AddDays(-1); while (1..5 -notcontains $dataAtual.DayOfWeek) { $dataAtual = $dataAtual.AddDays(-1) }; get-date $dataAtual -f yyyy.MM.dd}"
Call :RunPS %psCmd% FX
Echo %FX%
pause & Exit
::----------------------------------------------------------------------
:RunPS <PassPSCMD> <RetValue>
#for /F "usebackq tokens=*" %%i in (`Powershell %1`) do set "%2=%%i"
Goto:eof
:: End of :RunPS function
::----------------------------------------------------------------------

Alternate-split CSV Column Using Powershell [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
The task should be quite simple, yet I cannot figure out how to accomplish it.
Suppose I have a CSV file consisting of the following one-column data (there are actually hundreds of lines, not only six).
AAAAA
BBBBB
CCCCC
DDDDD
EEEEE
FFFFF
Using Powershell, I want to automate the creation of a new CSV file from the above file that splits the data into the following two tab-delimited columns.
AAAAA BBBBB
CCCCC DDDDD
EEEEE FFFFF
How can I achieve that? Thank You
What you show for the first file is not a CSV file, just a text file with values each on a new line.
The result you aparently want is a tab delimited file without headers.
In PowerShell, the easiest way to do that I think is to use an indexed loop like this:
$fileA = Get-Content -Path 'Path\To\The\File' # read the values as string array
$result = for ($i = 0; $i -lt $fileA.Count; $i += 2) { # loop through the lines in steps of 2
"{0}`t{1}" -f $fileA[$i], $fileA[$i + 1] # output the values delimited with a TAB character
}
# show in console window
$result
# write to file
$result | Set-Content -Path 'Path\To\The\New\File'
Output:
AAAAA BBBBB
CCCCC DDDDD
EEEEE FFFFF
If you want to create a real CSV file with headers, output objects instead of strings:
$fileA = Get-Content -Path 'Path\To\The\File' # read the values as string array
$result = for ($i = 0; $i -lt $fileA.Count; $i += 2) { # loop through the lines in steps of 2
# output an object with column headers
[PsCustomObject]#{ ColumnA = $fileA[$i]; ColumnB = $fileA[$i + 1] }
}
# show in console window
$result
# write to file
$result | Export-Csv -Path 'Path\To\The\New\File' -Delimiter "`t" -NoTypeInformation
Output:
ColumnA ColumnB
------- -------
AAAAA BBBBB
CCCCC DDDDD
EEEEE FFFFF
You used the batch-file tag, so I have to assume, a batch file solution is ok.
Read a line, remember it and print only every second run of the loop:
#echo off
setlocal enabledelayedexpansion
set "first="
for /f "delims=" %%a in (w.csv) do (
if not defined first (
set "first=%%a"
) else (
echo !first! %%a
set "first="
)
)
if defined first echo %first% -----
The last line takes care of odd line counts.
What follows is a batch file solution, which does things a little differently.
Essentially, if it's an odd line number, outputs the content followed by a horizontal tab, if its an even number, it outputs the line content followed by a carriage return and line feed. It also was written not to have issues outputting content beginning with a ; character, or containing ! characters.
Please adjust only your source, and destination files on lines 4 and 5 to your own absolute, or relative, filenames before testing:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F "Delims==" %%G In ('"(Set {) 2>NUL"') Do Set "%%G="
Set "{s}=C:\Users\Robith\Documents\input.csv"
Set "{d}=C:\Users\Robith\Desktop\output.csv"
If Not Exist "%{s}%" GoTo :EOF
For /F %%G In ('Copy /Z "%~f0" NUL') Do Set "{r}=%%G"
For /F "Delims==" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe OS Call /?
^| %SystemRoot%\System32\find.exe "=="') Do Set "{t}=%%G"
(Set {n}=^
% 0x0A %
)
( For /F Delims^=^ EOL^= %%G In ('Type "%{s}%"
^| %SystemRoot%\System32\find.exe /V ""') Do (
Set /A {i} += 1, {m} = {i} %% 2
Set "{l}=%%G"
SetLocal EnableDelayedExpansion
If !{m}! Equ 1 (Set /P "=!{l}!%{t}:~-1%"<NUL
) Else Set /P "=!{l}!%{r}%!{n}!"<NUL
EndLocal)) 1>"%{d}%"
For /F "Delims==" %%G In ('"(Set {) 2>NUL"') Do Set "%%G="

Remove folders from txt file in windows cmd

I have file with thousand lines like this.
x:/folder/folder/PN.xxx
y:/folder/PN.xxx
...
I need to make script for change this (for using in excel):
x;file.xxx
y;file.yyy
Best result for me is something this:
x;PN;x:/folder/folder/
y;PN;y:/folder/
I try to use easiest way in cmd:
D:\>powershell -Command "(gc serverPN.txt) -replace 'xxx', ';' | Out-File myFile.txt"
But I dont know, what is needet to write in XXX, when i want remove everything between /*/. Something like this can be close:"\\[\D]*\\'".
Thanks for help.
Supposing the lines are stored in text_file.txt, for a batch-file solution you need a for /F loop and the ~ modifiers for loop variable expansion (see also for /? for details):
for /F "usebackq delims=" %%L in ("text_file.txt") do (
echo(%%~dL;%%~nxL;%%~dpL
)
This reaults in the following output with respect to the sample data in your question:
x:;PN.xxx;x:/folder/folder/
y:;PN.xxx;y:/folder/
If you want the pure file name without extension in the output, replace %%~nxL by %%~nL.
In PowerShell try:
gc "serverPN.txt" | % {
get-item $_ | %{
"$($_.PSDrive);$($_.Name);$($_.Directory)" | Out-File "myFile.txt" -Append
}
}
This should be your requested regular expression: :.*/ and you can replace it with a ;:
powershell -Command "(gc serverPN.txt) -replace ':.*/', ';' | Out-File myFile.txt"
$test = 'C:/test1/test2/test3/test4/testfile.xls'
$newfile =$test.Substring(0,2) + ';' + $test.Substring($test.LastIndexOf('/')+1,($test.Length-$test.LastIndexOf('/')-1))
Output :
C:;testfile.xls
While it is not the beloved PowerShell, cmd could also produce the "best result".
C:>TYPE fi2.bat
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "usebackq tokens=*" %%f IN (`TYPE fi2.txt`) DO (
SET DRIVE=%%~df
SET DRIVE=!DRIVE:~0,1!
SET FILENAME=%%~nf
SET DIRNAME=%%~pf
SET DIRNAME=!DIRNAME:\=/!
ECHO !DRIVE!;!FILENAME!;%%~df!DIRNAME!
)
EXIT /B 0
C:>CALL fi2.bat
x;PN;x:/folder/folder/
y;PN;y:/folder/

powershell invoke-expression issue

I have a command to extract a file using 7z and it works in a DOS command line. The command is:
"C:\Documents and Settings\e.DEV\My Documents\7z.exe" x -o"C:\Documents and Settings\e.DEV\My Documents\utils" "C:\Documents and Settings\e.DEV\My Documents\rsasecureidtoken411.zip"
Now I have to run the upper command in powershell, I use a call operator & to call it (in Powershell command line), but it seems there are some errors with it. It just shows >> at the next line when I press enter, and I have to press Ctrl + c to stop the command.
PS C:\> & "C:\Documents and Settings\e.DEV\My Documents\7z.exe" x -o"C:\Documents and Settings\e.DEV\My Documents\utils" "C:\Documents and Settings\e.DEV\My Documents\RSASecurIDToken411.zip"
>>
>>
>>
>>
>>
>>
PS C:\>
I've also tried invoke-expression but still can't make it work. Anyone can help with it?
Thanks.
try this:
[string]$pathToZipExe = "C:\Documents and Settings\e.DEV\My Documents\7z.exe"
[Array]$arguments = "x", "-oC:\Documents and Settings\e.DEV\My Documents\utils", "C:\Documents and Settings\e.DEV\My Documents\RSASecurIDToken411.zip"
& $pathToZipExe $arguments