I need to replace every icon (AutoCAD 2010.LNK) found on the computer with another .LNK using batch.
The icon\ shortcut as we well know can be found anywhere and as many times as the user likes.
How can I achieve this?
first, read HELP FOR
and then try this in a command line
FOR /F "tokens=*" %a in ('dir /B /S "AUTOCAD 2010.LNK"') do ECHO COPY new.lnk %a
experiment with from various locations and test carefully
then create a bat file with the following contents. Note the change of %a into %%a and the removal of the 'echo'
#echo off
PUSHD C:\
FOR /F "tokens=*" %%a in ('dir /B /S "AUTOCAD 2010.LNK"') do COPY new.lnk %%a
POPD
#ECHO OFF
SET "linklist=%USERPROFILE%\linklist.txt"
SET "replacement=D:\path\to\replacement.lnk"
ECHO Searching...
DIR /B /S "C:\AutoCAD 2010.LNK" >%linklist%
DIR /B /S "D:\AutoCAD 2010.LNK" >>%linklist%
:: add similar rows for every drive letter you want to be included
ECHO Replacing...
FOR /F "tokens=*" %%f IN (%linklist%) DO COPY %replacement% %%f
ECHO Finished.
A couple of notes:
Your replacement shortcut file must be named differently (like AutoCAD 2010.LNK.new, for example).
In Windows Vista/7 you will probably be prohibited from overwriting files in certain folders unless you are running the script with elevated rights.
Related
I would like to recursively delete all files with a specific extension in a batch file.
I am aware of the following command:
del /s *.ext
However, this does on Windows also delete files with other extensions like e.g. .ext1 or .ext2 . The reason for this seems to be that the 8.3 file name of such a file ends with .ext and therefore also the files with longer extensions are matched.
I am looking for a replacement to the command above that recursively deletes all files with .ext extension but keeps files with longer extensions.
the where command works a bit differently (in regards to wildcards and short-names). Put a for /f loop around, and you're done. Your example would then translate to:
for /f "delims=" %%a in ('where /r . *.ext') do ECHO del "%%a"
Note: I disarmed the del command by just echoing it. Remove the ECHO after troubleshooting, when you are sure it does exactly what you want.
This also uses where.exe, but takes account of an issue not mentioned in another answer.
The issue is that where searches append each extension listed under %PATHEXT% to your .ext glob/spec. So whilst it will delete your target files, excluding files like .ext1 and .ext2 etc. it will now include for example, *.ext.com, *.ext.exe, *.ext.bat, *.ext.cmd, *.ext.vbs, *.ext.vbe, *.ext.js, *.ext.jse, *.ext.wsf, *.ext.wsh, and *.ext.msc etc.
The fix is to simply empty the content of %PATHEXT% before issuing the command. The following method does so within the For loop parenthsized command. As that is ran in another cmd.exe instance, it will not affect the instance in which the rest of your script resides:
#For /F "Delims=" %%G In ('"(Set PATHEXT=) & "%__APPDIR__%where.exe" /F /R "C:\SourceDir" "*.ext" 2>NUL"') Do #Del /A /F %%G
Obviously, you would modify, C:\SourceDir to contain the root location you require. The other current answers, use the current directory. If you want that, change it to ., or if you want the directory base as that of your batch file, change it to %~dp0.. Please do not remove any doublequotes.
Here are some alternative method examples, (please remember to adjust the drive/path/extension as needed)
If you wish to stick with the more traditional Dir command, then you could pipe the results through the findstr.exe utility, to exclude those matching the 8.3 names:
#For /F "Delims=" %%G In ('"Dir /B /S /A:-D "C:\SourceDir\*.ext" 2> NUL | "%__APPDIR__%findstr.exe" /I /L /E ".ext""') Do #Del /A /F "%%G"
You could also use the forfiles.exe utility for the task:
#"%__APPDIR__%forfiles.exe" /P "C:\SourceDir" /S /M "*.ext" /C "\"%__APPDIR__%cmd.exe\" /C \"If #IsDir==FALSE Del /A /F #File\""
Or this excruciatingly slow WMIC.exe utility method:
#"%__APPDIR__%wbem\WMIC.exe" DataFile Where "Drive='C:' And Path Like '\\SourceDir\\%%' And Extension='ext'" Delete 1> NUL 2>&1
Stephans answer is the shorter version, but you can use findstr's regex as well to match that the end of the name should be .ext
for /f "delims=" %%i in ('dir /b /s ^| findstr /IRC:"\.ext$"') do echo del "%%~i"
I have downloaded a series of torrent files, and want to delete some of the attached files that came within them. I'm familiar with forfiles but don't know how to set up a searchmask from a file of unwanted extensions (i.e. *.jpg, *.txt, etc.).
So far, I have captured 17 extensions that I won't ever need, and would hate to have to loop the entire batch program for an eighteenth time if I find another.
First, prepare a list of all the unwanted files with dir /b /s, capturing the output into a temporary text file;
dir /b /s *.txt /s *.jpg /s *.etc >%temp%\unwanted.lst
see help dir for an explanation on the /b and /s switches.
Then, delete the files in the list with a simple for /f over the contents of the captured list
for /f "delims=" %%a in (%temp%\unwanted.lst) do del %%a
See help for to understand what the /f does.
So, putting all the pieces together, your batch file would be something similar to this one:
#echo off
set "otf=%temp%\unwanted-%random%.lst"
dir /b /s *.jpg /s *.txt /s *.etc >%otf%
for /f %%a in (%otf%) do echo del "%%a"
echo del %otf%
note that it uses the %random% pseudovariable to minimize the risk of collisions
test in your situation and remove the echo commands
I am trying to create a batch file that will delete images with specific names. The images will have names such as
house-200x300.jpg
car-125x250.jpg
So what I need ideally is a regular expression to target files which end in -(Num1)x(Num2).jpg
Also, the images are in various folders and sub folders so I need to do this recursively from the parent folder.
Thanks
del /S *-???x???.jpg
Perhaps you may want to change del by dir /B command at first just to check that there is not any file that have not the specified file name format, but that will be selected by this wild-card.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*.jpg" ^|findstr /i /e /r /c:"-[0-9][0-9]*x[0-9][0-9]*\.jpg"'
) DO (
ECHO DEL "%%a"
)
GOTO :EOF
This should do the job - targeting only those filenames ending with -numXnum.jpg
You'd need to set your own sourcedir
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO DEL to DEL to actually delete the files.
Have you considered using powershell?
Regular expressions in batch files get very bad very fast as the syntax is limited.
My experience is limited, so there may very well be a better solution than this:
#echo off
FOR /F "delims=?" %%i IN ('dir /B /S ^| findstr /R "[^\.]*[0-9][0-9][0-9]x[0-9][0-9][0-9].jpg"') DO (
del /s "%%i" >nul 2>&1
)
I'm redirecting the output as I think that FOR starts to get confused about the output of del.
I needed to copy all the files from a directory tree into a single directory. A quick search provided me with this method:
for /f "tokens=*" %a in ('dir /b /s /a-d') do #copy "%a" "c:\Single-Folder"
I tried it, and it worked just fine. Deciding to simplify things a bit, I created a quick batch file so I didn't have to look this up in the future. my batch file looks like this:
set COPY_FROM="C:\Users\me\Desktop\Disc 1"
set COPY_TO="C:\Testing\test"
cd %COPY_FROM%
for /f "tokens=*" %a in ('dir /b /s /a-d') do #copy %COPY_TO%
pause
Unfortunately, when I execute this, I get the error:
C:\Users\me\Desktop\Tools>set COPY_FROM="C:\Users\me\Desktop\Disc 1"
C:\Users\me\Desktop\Tools>set COPY_TO="C:\Testing\test"
"\Users\me\Desktop\Disc 1"') was unexpected at this time.
C:\Users\me\Desktop\Tools>for /f "tokens=*" "\Users\me\Desktop\Disc 1"') do #copy "\Testing\test"
What works if I enter it into the command line does not work when run as a batch file. I did try replacing the variables with the actual path, but got the same error. Even when I create a batch file with only the line that works from the command line, it doesn't work when running from the file. Does anyone know what I am doing wrong?
Thank you in advance for any assistance.
#echo off
set "COPY_FROM=C:\Users\me\Desktop\Disc 1"
set "COPY_TO=C:\Testing\test"
md "%copy_to%" 2>nul
cd /d "%COPY_FROM%"
for /f "delims=" %%a in ('dir /b /s /a-d') do copy "%%a" "%COPY_TO%"
pause
I'm trying to create a standardised directory structure to prepare a drive ready for tidying and migration to a new location. What I'm looking to do is run a batch file in the root of the drive which works through all subdirectories and creates a new folder called 'Archive' in each of them ready for files to be tidied up and later moved or deleted.
I'm trying to use FOR /F with an MD command to make the folders, but I'm really struggling to understand the syntax re. tokens and variables. I'm trying to adapt from something like
for /f "tokens=*" %a in ('dir /b /s /a-d') do #copy "%a" "c:\Single-Folder"
which I nabbed from over here replacing #copy with #md... but I'm really out of my depth.
Any offers of help much appreciated
The command you are using:
dir /b /s /a-d
lists everything except directories (the - sign negates the attribute). So the right command you have to use is this:
dir /b /s /ad
(try it yourself!).
FOR /F reads the output of the dir command, one line of text at a time. tokens=* will get the whole line (removing leading spaces). I think you need this:
for /f "tokens=*" %a in ('dir /b /s /ad') do #echo mkdir "%a\Archive"
when you are sure it is right, remove the echo command to actually execute the mkdir.
I think what you want to do is this type of structure:
#Echo OFF
REM By Elektro H#cker
For /D /R "%SYSTEMDRIVE%\" %%# in (*) do (MKDIR "%SYSTEMDRIVE%\Single-Folder\%%~p#")
Pause&Exit