Parsing filename from text file gives the wrong details - windows

Please note I'm new to scripting so please be gentle!
I have a text file called list.txt with just one line (for testing), the line is:
D:\italy\gfm\users\test\avisgfm_1001_1500.txt
My script is:
#echo off
for /f "delims=" %%1 in (list.txt) do (
echo %%~1
echo %%~d1
echo %%~p1
echo %%~n1
echo %%~x1
)
However the result from the script is:
´╗┐d:\italy\gfm\users\test\avisgfm_1001_1500.txt
C:
\PCI\´╗┐d:\italy\gfm\users\test\avisgfm_1001_1500.txt
Any idea whats going on? I'm doing this via a Windows Batch file.
Thanks

The metavariable should be a (case-sensitive) letter.
Numbers refer to parameters to the routine.
for /f "delims=" %%a in (list.txt) do (
echo %%~a
and so on will work fine.

It seems you've saved list.txt with some encoding like UTF-8 with byte order mark at the head of file. BAT can process only ASCII text files. Try to create list.txt as simple ASCII text file. Notepad form Windows can do that.
%%1 works fine but it's better to use letter, for example %%i, as in for spec.

Related

bulk renaming files using batch file

I have a bunch of files (and more coming) that are downloaded from the internet (pdf printed) but they do come with a lot of names that I do not need/want; so I want to have a text file with the things that I want to remove. example
I have a text file with this two lines
Chapter 13. Text and Regular Expressions - Master-PowerShell _ With Dr. Tobias Weltner - PowerShell.com
- PowerShell Scripts, Tips, Forums, and Resources.htm
I want to keep adding lines of text to that file and then call it from another batch file to remove all the lines that are in this file, I got something working that I took from foxidrive here
I could keep adding strings to the rename, but I'm wondering if I could get all the lines to remove from a text file, and I know that there are programs out there (free ones) to do that, but I'd rather use the good old DOS batch.
Thanks
Here is my code
#echo off
if Exist %CD%/temp.txt del %CD%/temp.txt
echo #echo off >%CD%/temp.txt
echo for /f "delims=" %%%%a in ('dir /a:-d /o:n /b') do call :next "%%%%a" >>%CD%/temp.txt
echo GOTO:EOF >>%CD%/temp.txt
echo :next >>%CD%/temp.txt
echo set "newname=%%~nx1" >>%CD%/temp.txt
Echo.>>%CD%/temp.txt
for /f "delims=" %%l in (%CD%\To_remove.txt) Do (
echo set "newname=%%newname:%%l=%%">> %CD%\temp.txt)
Echo.>>%CD%/temp.txt
echo ren %%1 "%%newname%%" >>%CD%/temp.txt
Start Temp.txt
Exit
I did save it as txt and opened at the end of script to see how it looks, it's just a matter of renaming it to .cmd and works.
I had to add comments and edit my post to add it all together but works
Thanks

Edit text file with batch file under Windows

I've got an issue regarding a text file I'd like to change with a batch file. I was able to trim it to this point.
3539
78060031
523 )
What I need now is to get the numbers in the same line. By the way the text file is not written by my programm. What I need is now to get some backspaces till it looks like this:
353978060031523
I know there is a simple solution, but since I'm very bad in scripting I can't
find it.
Sorry for my bad english and the bad post!
It's the first time I post something here.
Thank you in advance.
This is a duplicated question. But, well, nevermind, I just answer your question.
I don't know what's the purpose of a ")" behind the "523", but since you're just concatenate the string, try out the following script:
#echo off
setlocal EnableDelayedExpansion
for /f "tokens=*" %%a in (hxh-chp.txt) do (
set "concatenate_string=!concatenate_string!%%a"
)
echo !concatenate_string!
pause >nul
The following should do what you expect, that is, concatenate the numerical characters and removing all spaces and the ):
setlocal EnableDelayedExpansion
for /F "usebackq" %%L in ("\path\to\your\text_file.txt") do (
set "CONCAT=!CONCAT!%%L"
)
endlocal & set "CONCAT=%CONCAT%"
echo %CONCAT%
This code makes use of the default behaviour of for /F, where the option tokens=1 and delims is tab and space.

Script hangs when executing in windows batch file

Hello I have a batch file that is used to move text file from on directory to another. The problem is that when there is a text file larger then 7 MB the scrip hangs and freezes the process, which leads to manually force the batch to end.
Why does this bat hang when it moves larger files then 7mb?. How can I solve this to let it move any size text file?
Thank you in advance for your help.
PS. TYPE was used because the original file is in ANSI/UNIX format and the only way we found to convert it to ANSI/PC was using TYPE.
cd /d "c:\users\you\"
for %%i in (*.txt) do (
echo processing %%i
TYPE "%%i" | MORE /P > "c:\temp\%%i"
del "%%i"
)
The purpose of the more command is to present only one page worth of text at a time. When the output device is not a console, the effective page length is very long but not infinite - it will still pause after writing 65534 lines.
Instead, try this:
(for /F "delims=" %%L in (%%i) do #echo %%L) > "c:\temp\%%i"
Command line breakdown:
for /F - read the contents of a file
"delims=" - don't treat spaces or tabs as delimeters
%%L - the variable (same as the %%i in the for command from your original script)
%%i - the file to read
#echo - writes the variable to standard output
( ) > file.txt - redirects standard output to the destination file

Get file name and append to beginning of line

I'm trying to get a side-by-side file path and file name in a text file so I can make inserting into a database easier. I've taken a look at other examples around SO, but I haven't been able to understand what is going on. For instance, I saw this batch file to append file names to end of lines but figured that I shouldn't ask for clarification because it's 1.5 years old.
What I have is a text file of file paths. They look like this:
\\proe\igi_files\TIFFS\AD\1_SIZE_AD\1AD0019.tif
What I want it to look like is this:
1AD0019.tif \\proe\igi_files\TIFFS\AD\1_SIZE_AD\1AD0019.tif
so that I can insert it into a database. Is there an easy way to do this on Windows via Batch files?
No batch file required. From the command line:
>"outputFile.txt" (for /f "usebackq eol=: delims=" %F in ("inputFile.txt") do #echo %~nxF %~dpF)
But that output format is risky because file and folder names can contain spaces, so it may be difficult to determine where the file name ends and the path begins. Better to enclose the file and path within quotes.
>"outputFile.txt" (for /f "usebackq eol=: delims=" %F in ("inputFile.txt") do echo "%~nxF" "%~dpF")
if done within a batch file, then percents must be doubled.
#echo off
>"outputFile.txt" (
for /f "usebackq eol=: delims=" %%F in ("inputFile.txt") do echo "%%~nxF" "%%~dpF"
)
You should read the built in help for the FOR command. Type help for or for /? from a command prompt to get help. That strategy works for pretty much for all commands.
In powershell, this little script should do the trick. In the first line, just specify the name of the text file that contains all the file paths.
$filelist="c:\temp\filelist.txt"
foreach($L in Get-Content $filelist) {
$i = $L.length - $L.lastindexof('\') -1
$fname=$L.substring($L.length - $i, $i)
echo ($fname + ' ' + $L)
}
If you don't have powershell installed on your machine, check out http://technet.microsoft.com/en-us/library/hh847837.aspx.
#ECHO OFF
SETLOCAL
(
FOR /f "delims=" %%i IN (yourfile.txt) DO ECHO %%~nxi %%i
)>newfile.txt
GOTO :EOF
No big drama - all on one active line, but spaced for clarity

For loop in batch file reading a file of File Paths

I want to write a Windows batch file script that will loop through a text file of FILE PATHS, do some work using data from each file path, then ultimately delete the file.
I started by running the FORFILES command and sending its output (the #PATH parameter is the full path of any file it matches) to a text file (results.txt).
I end up with a results.txt file like this:
"C:/Windows/Dir1/fileA.log"
"C:/Windows/Dir1/fileA.log"
"C:/Windows/Dir2/fileC.log"
"C:/Windows/Dir3/fileB.log"
What I want to do is:
Use a FOR loop and read each line in the results.txt file
For each line (file path), strip out the directory name that the log file is sitting in (ie: Dir1, Dir2, etc..) and create a directory with that SAME name in a different location (ie. D:/Archive/Backups/Dir1, D:/Archive/Backups/Dir2, etc..) -- assuming the directory doesn't exist.
Move the actual .log file to a zip file in that directory [I have code to do this].
Delete the .log file from its original location. [Pretty straightforward]
I'm having trouble figuring out the best way to accomplish the first 2 steps. My FOR loop seems to stop after reading the very first line:
FOR /F "tokens=1,2,3,4,5,6,7,8,9,10 delims=\" %%G in ("results.txt") DO (
...
)
You don't want to parse the path with the tokens/delims options because you don't know how many directory levels you are dealing with. You want to preserve each line in its entirety. TO parse the path you want to use the FOR variable modifiers. (type HELP FOR from the command line and look at the last section of the output)
%%~pG gives the path (without the drive or file name). If we then strip off the last \, we can go through another FOR iteration and get the name (and possible extension) of the file's directory by using %%~nxA.
The toggling of delayed expansion is just to protect against a possible ! in the path. If you know that no path contains ! then you can simply enable delayed expansion at the top of the script and be done with it.
EDIT - this code has been modified significantly since Aacini pointed out that I misread the requirements. It should satisfy the requirements now.
for /f "usebackq delims=" %%G in ("results.txt") do (
set "myPath=%~pG"
setlocal enableDelayedExpansion
for /f "eol=: delims=" %%A in ("!myPath:~0,-1!") do (
endlocal
if not exist d:\Archive\Backups\%%~nxA md d:\Archive\Backups\%%~nxA
rem ::zip %%G into zip file in the D: location
rem ::you should be able to create the zip with the move option
rem ::so you don't have to del the file
)
)
I wrote this to timestamp files before offloading to SFTP.
Hope you find it useful.
The timestamp coding may seem irrelevant to your issue, but I left it because it's a good example of dissecting the filename itself.
I suggest you put an ECHO in front of the REN command for testing. Different shells may have different results.
In the end, the delayedexpansion command wasn't necessary. It was the sub-routine that fixed my issues with variables inside the loop. That could possibly be because of my OS ver. (Win 8.1) - It wouldn't hurt to leave it.
#echo off
cls
setlocal enabledelayedexpansion
if %time:~0,2% geq 10 set TIMESTAMP=%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%
if %time:~0,2% leq 9 set TIMESTAMP=%date:~10,4%%date:~4,2%%date:~7,2%_0%time:~1,1%%time:~3,2%%time:~6,2%
echo TimeStamp=%TIMESTAMP%
echo.
for %%G in (*.txt) do (
set OLDNAME=%%G
call :MXYZPTLK
)
dir *.txt
goto :EOF
:MXYZPTLK
echo OldName=%OLDNAME%
ren %OLDNAME% %OLDNAME:~0,-4%_%TIMESTAMP%%OLDNAME:~-4,4%
echo.
:END
You have two minor problems:
The path separator in the file is '/' but you use '\' in the for loop.
The quotes around "results.txt" stop it working.
This works. Don't write quotes to results.txt and you won't get a quote at the end of the filename.
#echo off
FOR /F "tokens=3,4 delims=/" %%I in (results.txt) DO (
REM Directory
echo %%I
REM File
echo %%J
)

Resources