Echo a single number to all .dll files in a directory - text

In a folder named "drops" I have 3 .dll files. I am looking for a piece of code that can echo the number 0 to each .dll file without having to echo them individually. Basically, I don't want to have to open up each file and change the number to 0... laziness ensues!
for /f %%G IN (C:\Users\%USERNAME%\Desktop\CMDRPG\player\inventory\drops\*.dll) DO echo 0
I have tried using for. Is my code wrong? Thanks in advance.
Edit: Title didn't have anything to do with my question :D

Easy
forfiles /p "C:\Users\%USERNAME%\Desktop\CMDRPG\player\inventory\drops" /m "*.dll" /c "cmd.exe /c echo 0 > #path"
That will do it. Also, in ou code, add > %%G Thats the only mistake I found.
Mona

Drop /F, use
FOR %%G IN (C:\Users\%USERNAME%\Desktop\CMDRPG\player\inventory\drops\*.dll) DO ECHO 0
/F will try to open file *.dll.

Related

Batch - Find string in text file and delete full line

I use the script from this Thread accepted answer from Mofi. The script copy folders and store them in text file to exclude once copied folders on next run.
Sometimes I have folders called [incomplete]-different.names and I do not want to copy this folders. I want that all folders with the string [incomplete]- and the name behind are skipped or are not even written in the text file %CurrentList% for further processing.
These are my previous attempts but so far I could not get up and running with the script from the top.
Help would be nice, and thanks in advance.
Try 1:
for /f "delims=" [incomplete]-%%D in ("%CurrentList%") do (
set str=%%D
set str=!str: =!
set str=!str: %%D =!
echo !str!
Try 2:
findstr /v /b /c:"[incomplete]-"%%D" "%CurrentList%" del "%%D"
You were really close with your second attempt.
If all you want is to delete lines in %CurrentList% that contain the string [incomplete]-, you can just direct the output of a findstr /v to a temp file and then overwrite CurrentList with that file.
findstr /v /c:"[incomplete]-" "%CurrentList%" >tmpList.txt
move /y tmpList.txt "%CurrentList%" >nul

Search specified directory and output location of item(s) if found

Like the title says, I would like to know how to search a specified directory for a specified file using a for loop in a windows batch script. I can't use any 3rd party software.
Heres what I have at the moment:
for /R "%loc%" %%f in ("%searchterm%") do (
echo %%f
echo %%f>> "%genloc%wefound.txt"
)
%loc% is the location in which it should search.
%searchterm% is the file (e.g "hello.txt") that the script much search for.
It must output any files found (including their full path) on the screen and into the wefound.txt.
Let me know if you need more detail.
for /f "delims=" %%f in ('dir /s /b /a-d "%loc%\%searchterm%"') do (
echo %%f
echo %%f>> "%genloc%wefound.txt"
)
To get the for /r command recursively enumerate the files under a starting point, the file set must include some wildcard character (* or ?). As you know the exact filename, let the dir command do the work of searching the file.
for /R "%loc%" %%f in ("%searchterm%") do (
echo %%~Ff
echo %%~Ff>> "%genloc%wefound.txt"
)
For further details, type: FOR /?
Why complicate things with FOR? All you need is the DIR command:
dir /s /b /a-d "%loc%\%searchterm%>"%genloc%wefound.txt"&type "%genloc%wefound.txt"
The solution is even better if you can get your hands on a Windows port of the tee command (there are numerous free versions available). You could use my hybrid JScript/batch implementation of tee
dir /s /b /a-d "%loc%\%searchterm%"|tee "%genloc%wefound.txt"

Search and sort directory and subdirectory using Batch Script

I have been trying to do a search of the user directory and subdirectories in a primary school for all applications, music, videos and archived files to check what the kids are hiding from me. I have been using a basic dir script similar to this:
dir C:\Sample\*.exe, C:\Sample\*.rar, C:\Sample\*.mp3 /s /b /OE > D:\Result.txt
This provides me with an output of ALL files of those types sorted by user name.
What I want to know is, is there an easy way to either change the search to sort by type or to use the sort command afterwards to sort by the last character/s of the file.
Sample of output:
U:\adwan4\My Documents\My Videos\PhotoStory1.wmv
U:\ageeg2\My Documents\install_flashplayer11x32ax_gtbp_chra_aih.exe
U:\amcka55\My Documents\My Music\Angus.wmv
I have come up with something that appears to do the trick using Find along the lines of:
Find ".exe" D:\Result.txt >>D:\Sorted.txt
This appears to work fine for the avi, exe, etc. However I would be very happy for any suggestions.
Here you go
for %%x in (exe,mp3) do (
for /r C:\Dir %%a in (*.%%x) do echo %%a >>D:\Results.txt
)
For anyone interested, my initial solution (which took a lot more typing because it has no loops) looks something like this:
ECHO Off
dir U:\*.exe, U:\*.rar, U:\*.mp3 /s /b > D:\Result.txt
Echo Applications > D:\Sorted.txt
Echo --------------------------- >>D:\Sorted.txt
Find ".exe" D:\Result.txt >>D:\Sorted.txt
Echo Music >> D:\Sorted.txt
Echo --------------------------- >>D:\Sorted.txt
Find ".mp3" D:\Result.txt >>D:\Sorted.txt
Echo --------------------------- >>D:\Sorted.txt
Find ".wma" D:\Result.txt >>D:\Sorted.txt
etc
A lot more work to setup and is a 2 step process. The solution doesn't look as elegant as either option presented to me either. Thanks for your help.
The DIR /O sort option works fine when run without the /S option. But when you use the /S option it sorts the results within each directory individually, whereas you want to sort the entire result set.
Bali C's edited answer solves the problem by performing multiple searches individually, one per file extension. He chose to use the FOR command to iterate the files and ECHO the results. I believe it is simpler just to use multiple DIR commands.
>d:result.txt (
dir /s /b C:\Sample\*.exe
dir /s /b C:\Sample\*.rar
dir /s /b C:\Sample\*.mp3
)
or, perhaps a bit less typing
>d:result.txt (
for %%X in (exe rar mp3) do dir /s /b C:\Sample\*.%%X
)

Get filename from string-path?

How do I get the filename from this string?
"C:\Documents and Settings\Usuario\Escritorio\hello\test.txt"
output:
"test.txt"
I really tried to find this one before posting, but all the results were contaminated, they talk about getting filenames from current dir (I must work with strings only)
Method 1
for %%F in ("C:\Documents and Settings\Usuario\Escritorio\hello\test.txt") do echo %%~nxF
Type HELP FOR for more info.
Method 2
call :sub "C:\Documents and Settings\Usuario\Escritorio\hello\test.txt"
exit /b
:sub
echo %~nx1
exit /b
Type HELP CALL for more info.
if your path comes as a parameter, just use:
%~nx1 (1 is for the first param and we suppose it's the first one)
echo %~nx1 would return directly test.txt
Assuming you need the the names of files under the "c:\temp" directory tree (including sub-directories):
FOR /R c:\temp %i in (*.*) do echo %~nxi

Batch script: Search if a folder contains any files

I am trying to get a batch script to check whether a folder contains any files in it. So far this is how far I have got:
IF EXIST %FILEPATH%\%%i\FromGlobus\%FILE% (
%WINZIP% %FILEPATH%\GlobusEOD\ExtraFiles\%ZIPFILE% -m %FILE%
IF errorlevel 1 goto subBADEND
)
where %FILE% is *.* but what happens is it tries to zip up files even when none exist and therefore fails!
Any tips or ideas?
Thank you
After some tinkering with my own scripts
A variation of #Belisarius' answer is what I've been using for awhile...After tinkering for a bit, #NimeCloud's version just doesn't seem to work on my XP machine...
I've come up with a blend of the two that seems to work for my needs:
%FOLDER%=C:\Temp
FOR /F %%i in ('dir /b "%FOLDER%\*.*"') DO ( goto :Process )
goto :Exit
:Process
...
...
...
:Exit
exit
You may use something like
set VAR=init
for /f %%a in ('dir /b c:\kk\*.*') do set VAR=exists
if %VAR%==exists ...
Not very efficient in case of large directories, but it works.
HTH!
dir . returns . .. maybe these virtual dirs cause the mess. You could try FIND pipe like below:
SET filter=*.*
SET notfound="File not found"
DIR %filter% | FIND %notfound%
#If ErrorLevel 1 Goto :end
If you use /A:-D for dir command, then it will switch the return code if directory has files. Works only for files (/A:D won't work for subdirectories in the same way)
dir /A:-D /B "mydir" >nul 2>nul && echo.mydir having files
You could use find with dir to ensure the return code matches what you'd expect when no files are found:
DIR /A /B | >NUL FIND /V "" && ECHO Folder contains files || ECHO Folder empty

Resources