I am trying to create a batch script to move a set of files from one folder (root) to another and delete files of extension .dll in the root folder except one file. The command I tried is able to copy but not delete the files.
MOVE D:\mpgdata\sync\*.txt D:\data\sync\QDB_TXT_FILES
for %%i in (d:\data\sync*.dll) do if not "%%i"=="work.dll" del /f "%%i"
It had a case sensitive compare. /i fixes that.
There was also a missing backslash.
The %%~nxi makes it compare the filename only.
MOVE "D:\mpgdata\sync\*.txt" "D:\data\sync\QDB_TXT_FILES"
dir d:\data\sync\*.dll /b
pause
for %%i in (d:\data\sync\*.dll) do if /i not "%%~nxi"=="work.dll" del "%%i"
Related
I would like to open all files called "asdf.txt" in Notepad++, found in all subfolders from the directory that the batch file is located in.
The solution found here applies to file types but does not translate to exact file names:
Batch command to open all files of a certain type in Notepad++
Here is my attempt:
FOR /R %%a IN (asdf.txt) DO "C:\Program Files (x86)\Notepad++\notepad++.exe" "%%a"
This results in Notepad++ stating that asdf.txt doesn't exist and would I like to create it. It continues to ask me for every subfolder that exists (which can be hundreds of times). If I say yes, then it creates the file asdf.txt in every subfolder. If I keep pressing yes/no, then it eventually does open the existing files. What I want, however, is to open all existing files with exactly the same name and not create new files.
your filename doesn't contain any wildcard, so it's taken as a string, not a file mask.
Either add a wildcard:
FOR /R %%a IN ("asdf.txt?") DO "C:\Program Files (x86)\Notepad++\notepad++.exe" "%%a"
or use:
FOR /F "delims=" %%a IN ('dir /s /b /a-d "asdf.txt"') DO "C:\Program Files (x86)\Notepad++\notepad++.exe" "%%a"
You could also use the Where command:
#For /F "Delims=" %%A In ('Where/R <srcDir> asdf.txt') Do #Start "" "%ProgramFiles(x86)%\Notepad++\notepad++.exe" "%%A"
Change <srcDir> to your specific source directory, (use a dot for the current directory). You can of course remove the Start "" command if it doesn't suit your specific requirements
I have a bunch of folders, each containing a number of shortcut link files to mp3 files existing in completely separate folders. eg:
/rock-mp3-shortcuts
/jazz-mp3-shortcuts
/funk-mp3-shortcuts
what command would I run (or program to use) to copy all the underlying mp3 files back into the folders of shortcuts that are pointing to them.
I basically want to get all the files in each genre folder of shortcuts to then copy into my portable mp3 player.
This should work:
#echo off
FOR /r %%i in (*.lnk) do call :COPYFILE "%%i"
GOTO:EOF
:COPYFILE
set "filename=%1"
set "filename=%filename:"=%"
set "filename=%filename:\=\\%"
for /f "tokens=1* delims==" %%I in ('wmic path win32_shortcutfile where "name='%filename%'" get target /format:list ^| find "="') do (
set tatgetFile=%%J
copy /y "%tatgetFile%"
)
You'll have to create a bat file and paste my code into it. The file must be located in the folder where all your *.lnk (shortcut) files are. As you have three of them, you will have to copy the bat to each folder and execute it once. You also can automate this and use only one bat but I guess you'll figure out yourself how to do this. It will iterate over all shortcuts and copy the target files to the current folder.
Unfortunately, handling shortcuts in cmd is a pain in the 'a'. That's why we have to use wmic and win32_shortcutfile here.
You can check shortcutJS.bat with which you can create or check info about .lnk.You will need it in the same directory with this code:
#echo off
setlocal
::set your location on the line bellow
set "mp3_dir=c:\mp3_dir"
pushd "%mp3_dir%"
for /r %%# in (*.lnk) do (
for /f "tokens=1* delims=: " %%a in ('shortcutJS.bat -examine "%%~f#"^|find /i "target"') do (
echo location of %%# : %%~fb
rem !!!! remove the echo on the line bellow if everything is ok !!!!
echo copy "%%~fb" "%%~dp#"
)
)
endlocal
I need to create a batch script that will run all .exe files in a folder. This must include subfolders.
I am running windows 7, and the batch file is stored in the root folder
I have tried several variations with no success. The two main variations are as follows
REM dir *.exe /S /B > tmpFile
REM set /p listVar= < tmpFile
REM del tmpFile
FOR %%F IN (%listVar%) DO %%F
=======================================
FOR /F "usebackq" %%F IN ('dir *.exe /S /B') DO %%F
I have looked through several variations on the above methods but none work. The top version (which I would like to avoid) only reads in one line from the file. The Bottom version outputs an unmodified "dir" command in the prompt window.
Ultimately I would like help identifying how to solve this issue without the need for a temp file.
for /r "." %%a in (*.exe) do start "" "%%~fa"
This will start all the executable files under the current folder and subfolders. Change the reference to the current folder (".") to your needs.
for %1 in (%USERPROFILE%\*.exe) do "%1"
This will start all executables in your user folder. Of course, that includes installers and stuff like that, and you probably don't want to reinstall all your apps, so replace %USERPROFILE% with the directory you want.
I have a directory which looks like this:
\isa\documents\2004\2008\jac\file1.txt
\isa\documents\2004\jan\file1.txt
\isa\scannedDocs\2004\2009\jan\file2.pdf
\isa\documents\2005\2008\jac\file1.txt
\isa\documents\2003\jan\file1.txt
\isa\scannedDocs\2002\2009\jan\file2.pdf
I have a list of files I need copied (exported from a database), but because I don't need EVERY file in the directory, I only want the ones copied from my list:
Files-needed.txt:
\isa\documents\2004\2008\jac\file1.txt
\isa\documents\2004\jan\file1.txt
\isa\documents\2004\jac\file3.txt
\isa\documents\2003\jan\file1.txt
Basically:
I'll need to copy out the specific files needed, and
Log when a file can't be found, and,
I need folder structure retained, as the files might have the same name, just in different directories.
It's on a windows 7 machine, and I can run PowerShell and batch files. I tried robocopy and xcopy and either got all of the directories and no files or all files and no directories...
Any assistance would be great.
EDITS:
Okay, so i have tried Robocopy, but it is either copying the directories and no files or files and no directories. I haven't tried anything in powershell yet, but that might be the way...
It is Windows, I might have just written the slashes incorrectly above, I work between both environments, and was just trying to explain the problem.
Things I tried:
#echo off
set src_folder="C:\batchScripting\TEST_DIR\"
set dst_folder="C:\batchScripting\COPY2_DIR\"
robocopy "C:\batchScripting\TEST_DIR" "C:\batchScripting\COPY2_DIR" FileList.txt /S /V /NP /LOG:"log.log" /R:10 /W:30
and
#echo off
set src_folder=C:\batchScripting\TEST_DIR\
set dst_folder=C:\batchScripting\COPY2_DIR\
for /f "tokens=*" %%i in (File-list.txt) DO xcopy /s /i "%src_folder%\%%i" "%dst_folder%"
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
FOR /f "delims=" %%a IN (q23221996.txt) DO (
IF EXIST "%sourcedir%%%a" (ECHO f|xcopy /y "%sourcedir%%%a" "%destdir%%%a" 2>NUL >nul
) ELSE (ECHO "%sourcedir%%%a" does NOT exist)
)
GOTO :EOF
I used a file named q23221996.txt containing your data for my testing. sourcedir and destdir are both set up to suit my system.
The /y on the xcopy command forces overwrite if the destination file already exists.
I want to clean up my binary files in my development directory. Is there a way I can recurse through the directory structure and delete all files that are in a bin directory using the Windows command line? (I am using Windows 7.)
Per Nathan, I tried to make a batch foo.bat:
#ECHO OFF
for /R %%x in (.) do call:myExistFunc %%x
GOTO:EOF
:myExistFunc
if exist %~1\.\bin call:myDeleteFunc %%~1\bin
GOTO:EOF
:myDeleteFunc
echo. Deleting files from %~1
del %~1*.* /Q
...and this worked.
Create a batch file that contains the following:
for /R %%x in (bin) DO del "%%x\\*.*" /Q
This will recursively walk through all child directories (from the current directory) and delete all files from every BIN folder. You could change the *.* to whatever file type you'd like to delete.
This should work in a batch file:
for /R %%x in (bin) do if exist "%%x" del /q "%%x\*.*"
If running this command directly from the command line, replace all instances of %%x with just %x.
It's been awhile since I've had use any DOS but..
DEL BIN\*.*
That should delete the files in the directory named BIN below the current directory.
You should write a bat file:
for /r %%f in (*) do 'WHATEVER YOU WANT TO DO'