I am putting together a script for my media server. I need to run through all the
files in a directory and then ultimately create softlinks in another folder to act as a video playlist. I'm just starting out and I'm already having problems with my batch script
#echo off
SetLocal EnableDelayedExpansion
set TV="G:\TV"
FOR /R %TV% %%G in (.) DO (
Pushd %%G
Echo now in %%G
for /f %%f IN ("dir /b") do (
Echo %%f
)
Popd )
Echo "back home"
)
This produces a listing for each file but the filenames cut out after any spaces! It looks something like this:
now in G:\TV\UCB\UCB Season 3\.
Upright
Upright
Upright
Upright
...
now in G:\TV\Venture Bros\Season 3\.
File Not Found
now in G:\TV\Venture Bros\Season 4\.
The.Venture.Bros.S04E03.HDTV.XviD-2HD.avi
The.Venture.Bros.S04E04.HDTV.XviD-2HD.avi
The.Venture.Bros.S04E05.HDTV.XviD-2HD.avi
What can I do to fix this? Once I have the file it should be easy to create a soft link.
Just add quotes around your file names:
Pushd "%%G"
you may replace all your BAT with a simple oneliner FOR loop.
FOR /R G:\TV %%f in (*) do echo create soft link for %%f
Related
I put a for loop as below to my PC desktop for example, in order to move all files to a corresponding folder named by its extension.
for %%G in (*.*) do (
md "%%~xG" 2>nul
move "%%G" "%%~xG"
)
So far the script is ok just I want to improve it with the followings,
make folder only if it is not exist
make folder by parameter expansion will create a folder .jpg, but what i wanted is jpg, can I use something like set x=%%~xG, with setlocal and EnabledDelayExpansion, and then !x:~1! to trim the dot
Don't move the batch file itself to the bat folder created. Shall I use expansion like %f0 ?
Any help would be appreciated.
setlocal enabledelayedexpansion
for %%a in (*.*) do (
if not "%%~xa"=="" (
set "ext=%%~xa" & set "ext=!ext:~1!"
if not exist "!ext!" mkdir "!ext!" 2>nul
if not "%%~fa"=="%~f0" (
move "%%a" "!ext!\%%a" >nul
)
)
)
endlocal
While writing, I realised #Stephan 's comment is identical to this answer, so precedence to him!
I have a batch file for project backup, that i want to modify to exclude unnecessary files.
My current file uses simple 7z command to pack whole folder into one archive:
!7zipPath! a -t7z "archive_name" "path_to_add".
From what i found in other people questions, to exclude given files, i can use flag, for example -x!.txt, or -xr!.txt if path consist subfolders.
!7zipPath! a -t7z "archive_name" "path_to_add" -xr!*.jt
This should exclude all files with .jt extension (software specific extension), but -x!*.jt returns "incorrect wildcard typemaker *.jt
So from what i understand 7zip does not know this extension so i can not use it (is that correct thinking?)
So instead i tried to use include only. Answers from other question says that it should be simply added like this:
!7zipPath! a -t7z "archive_name" "path_to_add" ./*.xml
In my understanding this should create archive that includes only .xml files while keeping structure, but it just ignores that wildcard and put all the files from the path. It does not return any kind of error.
How can i use 7zip command options in batch file 7z command to get expected results?
PS; Will it work with extension on a folder? So that it would include whole folder.cojt for example?
EDIT: Full code
#echo off
setlocal EnableDelayedExpansion
SETLOCAL ENABLEEXTENSIONS
rem Get the time from WMI - at least that's a format we can work with
set X=
for /f "skip=1 delims=" %%x in ('wmic os get localdatetime') do if not defined X set X=%%x
rem dissect into parts
set DATE.YEAR=%X:~0,4%
set DATE.MONTH=%X:~4,2%
set DATE.DAY=%X:~6,2%
echo Today is: %DATE.YEAR%-%DATE.MONTH%-%DATE.DAY%
if not exist %DATE.YEAR%-%DATE.MONTH%-%DATE.DAY% (
echo Making Backup
For /F "Delims=" %%A In ('where /r "C:\Program Files\7-Zip" /f 7z.exe') do Set zipPath=%%A
IF NOT DEFINED zipPath (
echo Extending search range for 7zip...
For /F "Delims=" %%A In ('where /r C:\ /f 7z.exe') do Set zipPath=%%A
)
IF NOT DEFINED zipPath (
echo Extending search range for 7zip...
For /F "Delims=" %%A In ('where /r D:\ /f 7z.exe') do Set zipPath=%%A
)
IF NOT DEFINED zipPath (
echo 7zip not found on this PC
echo You can close this window now
cmd /k
)
md %DATE.YEAR%-%DATE.MONTH%-%DATE.DAY%
cd %DATE.YEAR%-%DATE.MONTH%-%DATE.DAY%
!zipPath! a -t7z "%DATE.YEAR%-%DATE.MONTH%-%DATE.DAY%_TuneCells" "M:\01\TuneCells\"
)
if exist %DATE.YEAR%-%DATE.MONTH%-%DATE.DAY% (
echo Backup already created %DATE.YEAR%-%DATE.MONTH%-%DATE.DAY%
)
PAUSE
As mentioned in one of commends, the search for 7z is there so that multiple people can easily use the code regardless of their installation path preferences (which are surprisingly wide)
I am trying to use Tesseract-OCR to read and OCR all .png files, not only in current folder, (as there is answer for that) but also in all subfolders.
This works for folder:
for %%A in ("C:\Users\x\AppData\Local\Tesseract-OCR\temp\*.png") do C:\Users\x\AppData\Local\Tesseract-OCR\tesseract.exe "%%~fA" "%%~dpnxA"
I tried with this to go through all subfolders that I have in "temp" folder:
(for /r %%a in (*.png) do C:\Users\x\AppData\Local\Tesseract-OCR\tesseract.exe "%%~nxa" "%%~dpnxA")
but I got this errors for every file:
C:\Users\x\AppData\Local\Tesseract-OCR\temp>C:\Users\x\AppData\Local\Tesseract-OCR\tesseract.exe "01.png" "%~dpnxA"
Tesseract Open Source OCR Engine v4.1.0-elag2019 with Leptonica
Error, cannot read input file 01.png: No such file or directory
Error during processing.
It is obvious that the script finds all files in all of the subfolders, but then it cant read then for some reason?
Also, this script works for one folder, but when I try to use with /r it doesnt go through all subfolders:
:Start
#Echo off
Set _SourcePath=C:\Users\x\AppData\Local\Tesseract-OCR\temp\*.png
Set _OutputPath=C:\Users\x\AppData\Local\Tesseract-OCR\temp\
Set _Tesseract="C:\Users\x\AppData\Local\Tesseract-OCR\tesseract.exe"
:Convert
For %%A in (%_SourcePath%) Do Echo Converting %%A...&%_Tesseract% %%A %_OutputPath%%%~nA
:End
Set "_SourcePath="
Set "_OutputPath="
Set "_Tesseract="
Any ideas?
Perhaps this sort of thing is what you're looking for:
#Echo Off
SetLocal DisableDelayedExpansion
Set "_SourcePath=%LocalAppData%\Tesseract-OCR\temp"
Set "_SourceMask=*.png"
Set "_OutputPath=%LocalAppData%\Tesseract-OCR\temp"
Set "_TesserFile=%LocalAppData%\Tesseract-OCR\tesseract.exe"
For /F "Delims=" %%A In (
'""%__AppDir__%where.exe" /R "%_SourcePath%" "%_SourceMask%" 2>Nul"'
) Do Echo Converting %%A...& "%_TesserFile%" "%%A" "%_OutputPath%\%%~nA"
Note, this assumes that tesseract allows for specifying the output directory and accepts doublequoted strings etc. It also assumes that you intend for all output files to be placed in %_OutputPath%.
If you wanted them to be placed along side their respective .png's then perhaps this will do it:
#Echo Off
SetLocal DisableDelayedExpansion
Set "_SourcePath=%LocalAppData%\Tesseract-OCR\temp"
Set "_SourceMask=*.png"
Set "_TesserFile=%LocalAppData%\Tesseract-OCR\tesseract.exe"
For /F "Delims=" %%A In (
'""%__AppDir__%where.exe" /R "%_SourcePath%" "%_SourceMask%" 2>Nul"'
) Do Echo Converting %%A...& "%_TesserFile%" "%%A" "%%~nA"
Can I rename all files in a folder with a batch file I tried this but it doesn't work
for %%a in (*.*) do ren sdel%random%.sdel %%a
and I also tryed
ren *.????? sdel%random%.sdel
Neither work what am I doing wrong?
Try this:
#echo off
setlocal EnableDelayedExpansion
for %%a in (*.*) do (
if not "%~nx0"=="%%a" ren "%%a" "sdel!random!.sdel"
)
Within a for loop, you should use EnableDelayedExpansion and use ! instead of % for variables. Note that I also added a if check so you don't rename the batch-file itself. If you wouldn't do that it would rename the batch-file, then be unable to find itself, and not rename any other files.
I'm very fond of listening to my music and I've recently found a need to organize my folders. I have my Music folder and inside it, I have folders in the format Genre\Artist\Album. I want to create a Favorites folder for every sub-folder in the Music folder, but I already have some Favorites folders created so:
I'm trying to create a batch script that adds a Favorites folder to every sub-folder with the exception of Favorites and Discography Info folders.
I know that something along the lines of this:
for /r "%windir%\Users\%username%\My Music" %%s in (.) do md "Favorites" "%%s"
...must be used but I don't know the necessary commands to create folders in multiple folders.
How do I create exceptions in the mass folder-creating process?
How do I extend the range of the command above?
#echo off
setlocal enableextensions disabledelayedexpansion
set "root=%windir%\Users\%username%\My Music"
for /r "%root%" %%s in (.) do if not exist "%%~fs\Favorites" (
if /i not "%%~nxs"=="Discography" if /i not "%%~nxs"=="Favorites" (
if /i not "%%~fs"=="%root%" echo md "%%~fs\Favorites"
)
)
endlocal
Adapt as needed. When output to console is correct, remove the echo command before md to create the directories
My batch scripting is a bit rusty but you can filter out strings using the findstr command. Depending on your needs, this should be good enough. It will add the folder to and path that doesn't contain the words Favorites or Discography:
set targetdir="%windir%\Users\%username%\My Music"
dir %targetdir% /ad /b /s > %temp%\#contents.txt
for /f "delims=" %%d in ('findstr /i /v "\<Favorites\> \<Discography\>" "%temp%\#contents.txt"') do md %%d\Favorites
#ECHO OFF
SETLOCAL
SET "targetdir=c:\destdir"
for /f "delims=" %%s in ('dir /b /s /ad "%targetdir%"') do SET tdir=%%s\&CALL :test&IF DEFINED tdir ECHO md "%%s\Favorites"
GOTO :EOF
:test
SET $test=%tdir:\Discography\=%
SET $test=%$test:\Favorites\=%
IF NOT "%tdir%"=="%$test%" SET "tdir="
GOTO :eof
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MD to MD to actually create the directories.
Note that when an attempt is made to create a directory that already exists, an error message will be displayed. This is harmless but ugly. You can cause the error message to be suppressed by appending 2>nul to each MD ... line.