Rewrite files after skipping lines batch file - file

Need to read files in directory, rewrite them after skipping the first 77 lines, save to new txt file with the old name.
Diectory (there are many more files than this):
{1061A083-F913-4F7A-AEC4-E89BD7FEAC47}.html
{275A93DF-997B-4B2B-B5D6-C66302A03508}.html
{41579A2D-C022-44BE-9752-5407D241BBE2}.html
{47339F9D-AC59-433F-9FEB-1E818C7C1904}.html
{513E7E93-F6D5-4F1F-A905-28FE4D3DB30C}.html
Code I have so far:
for /f "skip=7 delims=" %%a in ('dir /b *.html""') do (echo %%a>>newfile.txt)
xcopy newfile.txt C:\MBCNew\htmlFiles\Done\%%a.txt /y
del C:\MBCNew\htmlFiles\newfile.txt /f /q

The following simple batch script is very efficient (fast). It translates tab characters into spaces, but that should not be a problem for HTML.
#echo off
for /f "eol=: delims=" %%F in ('dir /b *.html') do (
more +77 "%%F" >"%%F.new"
move /y "%%F.new" "%%F"
)

Related

Batch create a Folder in every subfolders and Move all the .jpg files in it

I made this batch file that makes a folder named Pictures and puts all the .jpg files in it.
#echo off
md Pictures
for %%i in ("*.jpg") do (
move /Y "%%i" "Pictures" )
end
Is there anyway to make the script iterate through subdirectories as well? I want to run the batch file on a root directory and make it work for all subfolders.
Based upon the answer you've supplied, I would suggest you could do that like this, in a single line batch-file:
#For /D %%G In (*) Do #"%__AppDir__%Robocopy.exe" "%%G" "%%G\Pictures" *.jpg /Mov >NUL 2>&1
As an alternative, because For /D may not pick up all of your directories, (it ignores directories with the hidden and system attributes):
#For /F "EOL=? Delims=" %%G In ('Dir /B /AD') Do #"%__AppDir__%Robocopy.exe" "%%G" "%%G\Pictures" *.jpg /Mov >NUL 2>&1
Actually I just figured it out
#echo off
setlocal
for /f "usebackq tokens=*" %%a in (`dir /b /a:d`) do (
rem enter the directory
pushd %%a
echo In Directory: %%a
md Pictures
for %%i in ("*.jpg") do (
move /Y "%%i" "Pictures" )
rem leave the directory
popd
)
endlocal
It does the same thing but it works through subfolders. Took me sometime to figure it out. Anyway, hopefully it'll help others too.
Try this:
for /f %%a in ('dir /b /ad "Filepath"') do (
md "%%~fa\Pictures"
for %%b in ("*.jpg") do robocopy "%%~fb" "%%~fa\Pictures\" /mov
)

Rename file with different text position in batch

I am trying to rename text files with a different text position.
Example :
20170811191008_marie.txt --> marie_txt_20170811191008
I have a very basic rename batch command to rename files in loop but I am trying to find out how I can change the position of the text within the filename.
Command:
for /f "delims=" %%i in ('dir /b /a-d *.txt') do ren "%%~i" "%%~ni txt%%~xi"
Per your wish
for /f "tokens=1* delims=_" %%i in (
'dir /b /a-d *_*.txt ^|findstr /i "^20[0-9]*_.*\.txt$"'
) do echo ren "%%i_%%j" "%%~nj_txt_%%i"
> SO_45673483.cmd
ren "20170811191008_marie.txt" "marie_txt_20170811191008"
EDIT forgot to mention to remove the echo in front of ren to really execute the rename once you are shure it does want you want.
For multiple file extensions, you could always use something like this:
#Echo Off
SetLocal EnableDelayedExpansion
For /F "EOL=_ Tokens=1* Delims=_" %%A In ('Where .:??????????????_*.*') Do (
Set "fx=%%~xB"
Ren "%%A_%%B" "%%~nB_!fx:~1!_%%A" 2>Nul)
You can of course still specify .txt instead of .* but there would be no need to hard code _txt_ into the code following it.

batch file copy files from multiple sources and rename it by removing last 33 characters

i have batch file which need to copy 3 last modified files from 3 different sources and need to rename it by removing last 33 characters.
i made it in 2 files but from som reason when i put both codes together its not working...
my code :
#echo off
set folderpath=C:\Users\tzahi.k\Desktop\testSource\des
for /F "delims=" %%a in ('dir /b /od "C:\Users\tzahi.k\Desktop\testSource\source\*.txt"') do set Youngest=%%a
xcopy /y "C:\Users\tzahi.k\Desktop\testSource\source\%Youngest%" %folderpath%
for /F "delims=" %%a in ('dir /b /od "C:\Users\tzahi.k\Desktop\testSource\source2\*.txt"') do set Youngest=%%a
xcopy /y "C:\Users\tzahi.k\Desktop\testSource\source2\%Youngest%" %folderpath%
cd %folderpath%
for /f %%a in ('dir /b "%folderpath%\*.txt"') do (
set "fname=%%~na"
ren "%%a" "!fname:~0,-33!.txt"
)
pause
when i split the code in 2 files it works but i want it in one...
any suggestion?
i use
setlocal EnableDelayedExpansion
and it works

deleting all but one files using batch file not working

I have batch script that runs in WIn 7 command prompt.
it needs to delete all the files in folder except file with ".a" extension.
I tried following code
for /F %%I in ("*") DO if not (%%~xI=="a") del /q %%I
It deletes all files.
I tried following:
for /F %%I in ("*") DO if not (%%~xI==a) del /q %%I
no luck. Where am I wrong?
sedy
for /F loops consider sets in quotes to be strings, so you are saying to parse the string "*", which results in the delete command being interpreted as del /q *, which deletes everything.
Use for /F %%I in ('dir /b') instead, which will process based on the list of files in the current directory.
for /f "usebackq tokens=*" %%f in (`dir /b .\d\*`) do (
if not %%~xf equ .a (echo deleting %%f)
)

Command line loop through directories

I'm trying to write a batch file that will be run on a parent directory and will loop through subdirectories removing all but the newest 3 files from each subdirectory. What I have now recurses through the subdirectories but only skips the 3 newest files that it comes across, not the three newest files in each subdir. I think I need another loop in the code, but need help with where and what it should be. Help!
What I have so far - just ECHOing the output for now as a test.
#echo off
pushd "%~1"
for /f "skip=3 delims=" %%F in (
'dir /s /a-d /o-d /b') do ECHO del "%%F" /f
popd
You might try this:
#echo off
pushd "%~1"
for /D %%i in (*) do (
pushd "%%~i"
for /f "skip=3 delims=" %%F in (
'dir /a-d /o-d /b') do ECHO del /f "%%~F"
popd
)
popd

Resources