Using IF whiting a FOR in Batch - recursion

I need a script that checks the name of a CDG file and if is not present in the same folder another file with the same name in .mp3, then deletes the cdg file.
I thought to implement it with this simple code:
#echo off
for /R %1 %%f in (*.cdg) do (
if exist %%~nf.mp3 (
del %%f
)
)
But it return me with a syntax error in IF construct.
I tried to google it but it seems no-one had same problem (or i'm really bad in using google)
Can please someone tell me where i'm wrong?

Ok, heres an easy way of doing this with forfiles if your using Windows 7:
forfiles /m "*.cfg" /c "cmd /c if exist #fname.mp3 del #path"
That alone should work fine, and it's short enough to be typed directly in cmd.
Mona

It may be due to the name of the file containing spaces. If so then:
#echo off
for /R %1 %%f in (*.cdg) do (
if exist "%%~nf.mp3" (
del %%f
)
)
NB : Quotation Marks
Also, You should look into forfiles (since you'll only be needing one line of code) but that will only work if your using Windows 7.
Mona

Give this a whirl:
#echo off
for /R "%~1" %%f in (*.cdg) do (
if exist "%%~dpnf.mp3" (
del "%%f"
)
)

Related

Probleme with batch - script

i have 1000 photos with extension .jpg and every name of photo is a code like this 12345.jpg.
And i have 1000 folders with names like this: text_text_text_code(000000)
the job is put every photo in every single folder, i can solve this, but not working very well.
I found the problem, but i don't know how to solve.
in name folder i have text_text_text_code(000000), i need the code to stop when it arrives at character (.
#echo off
chcp 1250
echo.
echo.
cd "C:\Users\folderexample\Desktop\123"
for /r "C:\Users\folderexample\Desktop\imagem" %%a in (*.jpg) do (
for /f %%b in ('dir /b *"%%~na"*') do copy "%%a" "%%~fb"
)
I would like the search to go to the character ( and go to next search up until finish photos.

Get filename and file size from directory without using recursive switch

I am trying to write a command script to do the following. For each file in a specific directory, I want to get the filename and the size of the file. If the filename is what I am looking for and the file size is greater than zero, I want to process the file.
I can get the filename fine but I cannot get the file size without using the recursive switch.
This code works but I don't want it to look in subdirectories:
for /f "delims=" %%f in ('dir /s /b /a-d "%input_directory%" ') do (
set filename=%%~nxf
set filesize=%%~zf
)
I've looked around but have been unable to find what I need. Any help would be appreciated.
I finally figured it out. Here's what I used to get the job done.
for /f "tokens=*" %%f in ('dir /b "%input_directory%" ') do (
set filename=%%~nf
if !filename!==somename (set validname=Y)
if !validname!==Y (
for %%a in ("%input_directory%\!filename!") do (
set filesize=%%~za
if !filesize!==0 (
log as empty file
) else (
process file
)
)
)
)

Compare directory name to string

I've created this very simple batch file for the sake of testing a concept I'm hoping to utilize. I need to recursively delete all of one type of file except in folders with a specific name. Here's my code:
:recur
FOR /f %%a IN ('DIR /b') DO (
IF EXIST %%a\NUL (
IF ["%%a" NEQ "subtest2"] (
ECHO %%a
CD %%a
CALL :recur
CD ..
)
)
COPY "*.cfm" "*_copy.cfm"
REM DEL "*_copy*.cfm"
)
Right now I'm just testing using copy instead of delete. Basically, this should create a copy of all the .cfm files except in the folder "subtest2". Right now it's recursively making the copies everywhere, including subtest2. How do I stop this?
The structure of my base directory is:
TestFolder
---subtest1
------test.pdf
------test.txt
------test.cfm
---subtest2
------test.pdf
------test.txt
------test.cfm
---test.pdf
---test.txt
---test.cfm
---recur.bat
The square brackets are not balanced on both sides of the IF comparison, so it can never match. The brackets are not part of the IF syntax. If present, they simply become part of the string that is compared. The same is true for any enclosing quotes. Remove the square brackets, and it will work (assuming there are no other problems)
Here is a simple method to accomplish your goal. I've prefixed the DEL command with ECHO for testing purposes:
for /r /d %%F in (*) do echo %%F\|findstr /liv "\\subtest2\\" >nul && echo del "%%F\*.cfm"
The FOR /R /D simply recurses all folders. The full path of each folder is piped into a FINDSTR command that looks for paths that do not contain a \subtest2 folder. The ECHO DEL command is only executed if the \subtest2\ folder is not found in the path.
Remove the last ECHO when you have confirmed the command gives the correct results.
Change %%F to %F if you want to run the command on the command line instead of in a batch file.
for f in `find . -path YOURDIR -prune -o print`
do
rm whateveryouwanttodelete
done
the find command in backticks finds all files but ignores the directory -prune you want to ignore. Then in the body of the loop you nuke the files. You can do even better with
find . -path YOURDIR -prune -o -print0 | xargs -0 rm -f
no need for the loop. DISCLAIMER: I haven't tested it so perhaps you want to start adopting it with cp instead of rm.
You can try this:
#echo off&setlocal
for /r /d %%i in (*) do (
pushd "%%i"
echo(%%i|findstr /ri "\\subtest2$" || COPY "*.cfm" "*_copy.cfm"
popd
)

Batch file: get file timestamp date only and age in days

The following few lines output the names of certain files in a folder, a delimiter, and a timestamp.
for /f "eol=: delims=" %%F in (
'dir /b /a-d /one *.txt *.pdf *.doc* *.xls* *.msg 2^>nul'
) do echo %indent%%fileBullet% %%F%delimeter% %%~tF
So, produces something like this
Response.docx; 02/07/2013 12:13 PM
I'd like to remove the time portion of the timestamp (so date only), followed by how many days old the file is. So
Response.docx; 02/07/2013; 14
I've found some fairly lengthy solutions online that contain a dozen or so lines. Is there a short and sweet approach?
Here's something shorter and sweeter. It's not as short and sweet as you'd like, but at least it's not 12 lines of code. :)
for /f "eol=: delims=" %%F in (
'dir /b /a-d /one *.txt *.pdf *.doc* *.xls* *.msg 2^>nul'
) do call :datediff "%indent%%fileBullet% %%F%delimeter%" %%~tF
goto :EOF
:datediff
echo wscript.echo DateDiff^("d", "%2", Date^(^)^)>"%temp%\dd.vbs"
set /P i="%~1 %2%delimeter% "<NUL
cscript /nologo "%temp%\dd.vbs"
del /q "%temp%\dd.vbs"

ASP.net: Read HTML and ASP.net Pages to Strings?

I am trying to write an aspx page which will crawl through a directory and find all the files contained within. I think I have that part down.
Is it possible to read to a string without first creating .txt files from the html and asp pages I'm reading through? I don't want to create a ton of new files and then end up having to delete them later.
Ultimately, I'm trying to develop a tool to search through an entire directory and find all the image tags which have empty alt attributes or no alt attributes. I wrote some jQuery which can find the tags, and I have also written the part that searches through a directory.
If you have a file on your filesystem, you can simply read it - if you know it is a textual format, you need to use a stream with the correct encoding to do this.
Since you are reading and querying HTML, I suggest using a library that is specifically written for this task - the HTML Agility Pack - you can give it the path to the HTML file and then query it for all img elements. The source download comes with sample projects that will show you how to achieve this and other tasks.
Link:
http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx
Example:
http://www.csharp-examples.net/load-text-file-to-string/
(this example shows how to work with .txt files, but I believe if you can put any other extension)
Getting all files:
http://www.csharp-examples.net/get-files-from-directory/
Edit: and don't forgot about encoding.
sure, why not save to environment variables, no fuss, no mess. so try something like this:
will take apart an html or asp file and save to an array of variables, i have shown you how to put it back together as well.
let me know how if this is a solution for you
#echo off
setlocal EnableDelayedExpansion EnableExtensions
echo.
set count=0
if exist newfile.html del newfile.html
:: to unassemble
for /f "tokens=*" %%a in (filename.html) do (
echo %%a
set /a count=count + 1
set htmllinenum!count!=%%a
)& set finalcount=!count!
:: to assemble
for /l %%a in (1,1,%finalcount%) do (
echo !htmllinenum%%a!>>newfile.html
)
notepad newfile.tmp
set count=0
if exist newfile.asp del newfile.asp
:: to unassemble
for /f "tokens=*" %%a in (filename.asp) do (
echo %%a
set /a count=count + 1
set asplinenum!count!=%%a
)& set finalcount=!count!
:: to assemble
for /l %%a in (1,1,%finalcount%) do (
echo !asplinenum%%a!>>newfile.asp
)
notepad newfile.asp

Resources