Hello I need a batch for the following process:
A ton of files from sub folders must use the converter to convert filetype from .m2 to .m2_lk
without copying the files in a new folder or something like this.
For example file "tree.m2" is in an sub folder of an sub folder must use the "converter.exe"
I have tried this code but it doesn't work:
FOR /D "X:\XYZ\M2\ToLkM2\" %%G IN (*.m2) DO converter.exe %%G
It looks to me like you simply used the wrong FOR option. You want /R, not /D. Also, The file spec should be quoted on your conversion command line, just in case there are spaces or special characters.
FOR /R "X:\XYZ\M2\ToLkM2\" %%G IN (*.m2) DO converter.exe "%%G"
for /f "tokens=*" %%i in ('dir X:\XYZ\M2\ToLkM2\ /b /s^|find ".m2" /i') do converter.exe %%i
You can try using FORFILES command:
FORFILES /s /M *.m2 /C "cmd /c converter.exe #file #file_lk "
Option /s enables recursing to subdirectories. Read here about many file related substitutions that can be used in command section.
Related
what im looking for is a .bat file code to zip files individually in all subfolders in the current folder and then delete the of files after, to exclude already zipped/compressed files, what i dont want is folders to be zipped and i want the files to keep there name when zipped
i have a bunch of folders/files and the only code i found
#ECHO OFF
FOR %%i IN (*.*) DO (
ECHO "%%i" | FIND /I "batch zip files.bat" 1>NUL) || (
"c:\Program Files\7-Zip\7z.exe" a -tzip "%%~ni.zip" "%%i"
if %ERRORLEVEL% == 0 del "%%i"
)
)
zips all files in the current directory and doesnt touch subfolders
i'd appreciate it if anyone can do this for me as i can save a ton of space with all files zipped
The first issue you have with your provided code is that your For loop is only parsing files in the current directory, there is no recursion into subdirectories. To parse files within the subdirectories, I'd advise that you use a For /F loop, with the Dir command using its /B and /S options. I would also advise that you include the attribute option, /A, which will include every item, then omit those which you're not interested in. For instance, it's unlikely that you want to zip the directories, hidden files, reparse points, or system files. You can do that by excluding those attributes, /A:-D-H-L-S. To learn more about the For command, and the Dir command, open a Command Prompt window, type for /?, and press the ENTER key. You can then do the same for the Dir command, i.e for /?. As you have not defined a working directory at the start of your script, it will run against every file and directory in whatever is current at the time you run it. Because your code has a line excluding a file named batch zip files.bat, I'm going to assume that is the name of your running script, and that your intention is to therefore run the script against everything in the tree rooted from the same location as the batch file itself. To ensure that is always the case, for safety, I've defined that directory as the current directory from the outset, using the CD command, CD /D "%~dp0". %0 is a special batch file argument reference to itself, to learn more about this please take a look at the output from both call /?. You can also learn about the CD command entering cd /?, in a Command Prompt window too. To also omit your batch file, as you don't want it to be zipped and deleted, I've piped the results from the Dir command through FindStr, printing only items which do not exactly match the case insensitive literal string %~f0 (expanding to the full path of the batch file itself). Additionally, I've piped those results through another findstr.exe command to omit any files already carrying a .zip extension, as there's no point in zipping files which already zip files. (Please note however, that for more robust code, you should really check that those are zip archives and not just files carrying a misleading extension). The results from those commands are then passed one by one to the Do portion which includes your 7z.exe command. I've assumed at this stage, that your intention was to save the zipped archives to the same location as the originating files. To do that I've used variable expansion on %%G to stipulate its directory, path, and name, %%~dpnG, (see the usage information under for /? to recap). Upon successful completion of the archiving process, the original file will be deleted, to do that I appended the -sdel option to your original command string. Please be aware that you may want to include additional options, should you wish to update existing zip files etc. (use "%ProgramFiles%\7-Zip\7z.exe" -? in a Command Prompt window to see them). As I've not mentioned it previously, at the beginning of the script, I made sure that extensions were enabled. Whilst it is the default option, it's safer to be sure, as variable expansion and the commands CD, and For can be affected, if they're not.
Here's the code as explained above:
#Echo Off
SetLocal EnableExtensions
CD /D "%~dp0"
For /F "EOL=? Delims=" %%G In ('Dir "*" /A:-D-H-L-S /B /S 2^> NUL ^|
%SystemRoot%\System32\findstr.exe /I /L /V /X "%~f0" ^|
%SystemRoot%\System32\findstr.exe /E /I /L /V ".zip"'
) Do "%ProgramFiles%\7-Zip\7z.exe" a -tzip "%%~dpnG.zip" "%%G" -sdel
Looking at your question, which has changed from what you'd asked initially, you appear to not be interested in the files of the batch file directory any more, "zip files individually in all subfolders in the current folder". For that reason, I've provided the following alternative, methodology.
The difference is that I first of all use a For loop to include only directories in the current working location, /A:D-H-L-S, before running the same method used in my previous example, but with one difference. As we're now no longer zipping files in the current working directory, we can remove the findstr.exe command filtering out the running batch file:
#Echo Off
SetLocal EnableExtensions
CD /D "%~dp0"
For /F "EOL=? Delims=" %%G In ('Dir "*" /A:D-H-L-S /B 2^> NUL'
) Do For /F "EOL=? Delims=" %%H In ('Dir "%%G" /A:-D-H-L-S /B /S 2^> NUL ^|
%SystemRoot%\System32\findstr.exe /E /I /L /V ".zip"'
) Do "%ProgramFiles%\7-Zip\7z.exe" a -tzip "%%~dpnH.zip" "%%H" -sdel
Please be aware, that my answers above are to essentially correct your code attempt, and not a personal recommendation for speed, or in performing the task laid out in your question. Additionally, I have no idea what will happen if any of those files are in use/locked, and have made no attempt at checking for such scenarios.
I am using the following script lines in a batch script (.bat) to copy the contents of a directory (foo) to another (bar):
move "C:\foo\*.*" "C:\bar\"
for /d %%a in ("C:\foo\*") do move "%%~fa" "C:\bar\"
The first line moves files and the second lines moves folders. However, these aren't moving the hidden directories. .git is a common example. I tried for /d %%a in ("C:\foo\.*") do move "%%~fa" "C:\bar\" with no success.
How can I move my hidden directories along with the rest of my files and directories?
EDIT: The following solution is very close to doing what is required, but fails because the "move" command can't find the hidden folder (tried the same on a .folder that wasn't hidden and it worked):
for /f "tokens=*" %%G in ('dir /b /a:hd "C:\foo\*"') do move "C:\foo\%%G" "C:\bar\"
For what you are trying to do, you can first use the attrib command before moving things to remove hidden attributes from files. You can use this to accomplish your goal:
attrib -h "C:\Program Files\Git\usr\tmp\*.*"
move "C:\Program Files\Git\usr\tmp\*.*" "C:\Program Files\Git\usr\bin\"
To do this with other things, you can do this:
attrib -h "<SourceParentFolder>\*.*"
move <source> <destination>
NOTES: You should note #Mark's comment. Using C:\file\path\folder\* is not correct. You should use C:\file\path\folder\*.*. For more information view #Mark's comment
For more information on attrib use attrib /? or check this
A file or directory name beginning with . does not mean that it is hidden.
Anyway, for/for /D iterates over non-hidden files/directories. However, dir allows to return hidden items as well when using its /A option, which can be made use of by using for /F:
rem // Change into source directory:
pushd "C:\Program Files\Git\usr\tmp" && (
rem // Iterate over all directories, even hidden and system ones:
for /F "delims= eol=|" %%I in ('dir /B /A:D-S-L ".*"') do (
rem // Actually move the directory:
move "%%I" "C:\Program Files\Git\usr\bin\"
)
rem // Return from source directory:
popd
)
After some extra research, I found that robocopy seems to be included by default in Windows 10 distributions and robocopy /MOVE allows moving all the needed files and folders in a single line, such as:
robocopy "C:\foo" "C:\bar" /E /MOVE
Additional logging options can be added to reduce the output to the command line.
I am trying to find specific files within a folder and its sub-folders and copy them to a new folder. I used to use this batch file for this purpose but now I get this error :
The system cannot find the file specified.
Here's the batch-file content:
pushd "\\internal.company.com\path\"
md myfile
FOR /R "\\internal.company.com\path\" %%G in (prefix_myfile*) do copy %%G "\\internal.company.com\path\myfile"
Any input is appreciated.
UPDATE
I tried printing %%G like this:
FOR /R "\\internal.company.com\path\" %%G in (prefix_myfile*) do echo %%G
and it works well. The problem arises with copy command which cannot read %%G as an argument.
I'd change to
... copy "%%G" ...
which would cater for spaces in %%G.
Perhaps you should determine whether the problem is with the for/r or %%G. If you simply display %%G with an echo %%G command, then you may see some problem. If it still won't run, then the for /r is the problem.
Since you are pushding to the appropriate directory, there is no need to specify the target directory in the for/r. It can be omitted or replaced by .
or perhaps echo %cd% directly after the pushd to show whether the pushd is the cause of the problem.
I realized that this would work for me (executed in cmd). However, it is plausible because I don't have any *.txt file in the main directory. Not the best solution but a workaround.
pushd "\\internal.company.com\path\"
rem first copy the desired files (text files) to the main-folder
for /f "tokens=*" %f in ('dir /a:-D /s /b myfile*') do copy "%f"
rem then make a new-folder and move them to there
md myfile
move *.txt "\\internal.company.com\path\myfile"
Note: if you want to execute this as a batch file, you need to use %%f instead of %f.
This will copy the files into main folder and then moves them into the desired sub-folder.
I would like to make a batch script that can delete multiple directories at once. I've searched the web, but only seem to find methods for deleting multiple sub-directories or files. So far I can make a script to delete one specific folder using this structure:
rmdir "C:\Users\%USERNAME%\AppData\Roaming\Microsoft\Windows\Cookies" /S /Q
I then tried a multitude of methods to declare other folders in different combinations and sequences but none seem to work. First I tried putting all other directories in a single line followed by /S /Q, then placing rmdir and /S /Q at the start and end of each new path, then putting each path on a new line by pressing enter. Am I using the wrong commands? Any help is appreciated.
for %%a in ("dirname 1" "dirname2" "as many as you want") do rd /s /q "%%~a"
should do what you want - %%a is set to each [optionally-quoted] argument in turn. You Must use the quotes if the directoryname contains separators like spaces.
You can just use a for loop to iterate over a list of directories, as per:
pax> for %d in (c:\dir1 c:\other\dir2) do echo %d
c:\dir1
c:\other\dir2
In your particular case, it would be something akin to (in a cmd file):
for %%d in (c:\dir1 c:\other\dir2) do rmdir "%%~d" /s /q
You can find subdirectories by name automaticly and delete them
for /f %%d in ('dir /b "C:\exampledir\searched_dir_name*"') do rd /s /q "C:\exampledir\%%d"
this command will delete all directories with name "searched_dir_name" in directory C:\exampledir\
I wish to copy a file with extension .dyn which is located in each subfolder of main folder(T15_finished). I wish to copy it into respective subfolder at other location(T15). I have created that location using xcopy command. Here, .dyn file is being copied successfully in respective subfolder in T15 folder(see below code). Now, I have a file which has extension as .dynain which is located in the same subfolder as .dyn. And .dynain file is also getting copied which i don't want.
Please see following code which i have created. Can anyone tell me whats wrong ?
#echo off
xcopy D:\Master\SPRINGBACK\CPW\T15_finished D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15 /t
xcopy /e D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15
pause
Short file names. If you do a dir /x in the folder containing the .dynain file, you will see the 8.3 filename generated for the file, and it will have .dyn extension.
If you know the extensions of the conflicting files, you can use robocopy with /xf switch to indicate the files (*.dynain) to exclude, or you can generate a exclude file to use with xcopy /exclude:file (see xcopy /? for a explanation)
Or, you can generate the list of files to exclude
(for /f "tokens=" %%a in (
'dir /s /b "D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn" ^| findstr /v /i /e /l ".dyn"'
) do #echo(%%~nxa)>excludedFiles.txt
xcopy /exclude:excludedFiles.txt /e D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15
Or (as posted by foxidrive), copy all and then delete the non needed files.
The short filename is being matched as well as the long filename. That is the reason.
A solution is to use another command to delete the files:
del /s "D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15\*.dynain"