I need to compare filenames (minus extension) in folder A with foldernames in folder B.
If the filename minus extension has a matching foldername in folderB then move it to FolderC.
Example:
I have 3 directories: A, B and C
In folder A there are *.txt files.
In folder B are several folders
if a foldername (in folderB) has the same rootname as a filename in folderA then move the folderA file to folder C.
This should do it from the cmd prompt. Double all % to %% if it is inside a batch file.
for /d %a in ("folderb\*") do if exist "foldera\%~nxa.txt" move "foldera\%~nxa.txt" "folderc"
Answer 2
Your task (in this answers comments) can be turned around, to delete the .apk files that do not have a matching folder in _Dicts folder.
If that is what you need to do then test this:
#echo off
for %%a in ("_INPUT_APK\*.apk") do if not exist "_Dicts\%%~na\" del "%%a"
Related
I have about 150 folders with .7z files that I want to extract all the .7z files in order into a different directory, "Z:\master statistics" and automatically overwrite. Below is the batch I'm working with, but I am unsure how to extract the .7zfiles and skip the first parent folder in the .7z.
For example:
sales2015.7z file has the below directories
sales/engines/parts
sales2010.7z file has the below directories
sales/equipment/parts
When it extracts, I am trying to get it to extract from the second directory. So if the batch worked correctly, the output directory would only have the Engine & Equipment folders and all its subdirectories. If a file with the same name exists, it automatically overwrites.
FOR /D /r %%F in ("*") DO (
pushd %CD%
cd %%F
FOR %%X in (*.7z) DO (
"C:\Program Files\7-zip\7z.exe" x -o "%Z:\master statistics%"
)
popd
)
I have about 10 batch files which copy *.txt files from root folder to their subfolders. The code for copying is following:
cscript ..\myScripts\CopyFiles.vbs "." "..\myScripts\Copy.cfg" "scripts_A" "((.+\.txt)|(.+\.ps1))"
Every single batch file copy *.txt files to folder with the same name (A.bat ->scripts_A, B.bat ->scripts_B). I need to have right filename in each subfolder (in folder scripts_A files need to be operationAfirst.txt, operationAsecond.txt, in folder scripts_B files operationBfirst.txt, operationBsecond.txt). So the result should be following:
for folder scripts_A:
operationAfirst.txt
operationAsecond.txt
for folder scripts_B:
operationBfirst.txt
operationBsecond.txt
All *.txt files in subfolders are usually clones of *.txt files for folder scripts_A. I just need to update their filename. Is there any posibility how to edit batch file, which can replace "A" string in filename for "B" in batch used for folder scripts_B?
Instead of repairing the outcome of a poorly designed setup, I'd prefer to give hints how to alter that situation. So against the best of one's knowledge this batch will do the rename:
#Echo off
Setlocal
CD /D X:\where\ever\you\start
For %%A in (A B C D E F G H I J) Do For /F "delims=" %%B in (
'Dir /B %CD%\Script_%%A\operation*.txt |Findstr /V "operation%%A"'
) Do Call :RenFiles %%A "%%~fB"
Goto :Eof
:RenFiles
Set OldName=%~nx2
Set NewName=operation%1%OldName:~10%
Echo Ren %2 %NewName%
Change the path in the cd statement.
The first of the stacked for loops iterates the letters A to J.
The second one executes dir in the folder script_(letter) listing all
files starting with operation and the extension .txt. The findstr
then removes all correct names for this folder.
The remaining are then arguments to the called subroutine
The subroutine builds the new name from the prefix operation
followed by the folder letter and the original name from 11th place
(offset 10).
As long as the echo remaines in front of the ren it only shows what it would do.
I search for a way to unpack multi-volume archives after download via batch.
I download folders with .r?? files in it over a FTP monitoring program and want that WinRAR goes in the first subfolder in the source folder and start unpacking .r00, delete the archive and move the folder with the unpacked files to a new location.
Then the batch script should start this process again with the next subfolder.
Let's say the source folder C:\Users\unpack contains following subfolders with files:
source folder
subfolder1
Archive1.r00
Archive1.r01
Archive1.r02
xxx.txt
subfolder2
Archive2.r00
Archive2.r01
yyy.txt
subfolder3
Archive3.r00
Archive3.r01
Archive3.r02
Archive3.r04
Archive3.r05
zzz.txt
I had start to do this with the script in the link below, but that script can't do what I want, so I have started a new question.
How to unpack all rar archives in all subfolders of a folder and then delete the archives?
The script in the link above unrars all files in all subfolders and then moves the folder with its files to a new location. I want that the script unrars and moves subfolder for subfolder in the source folder.
Edit.1
If winrar is ready with the first subfolder the structure in the source folder should look like this:
source folder
subfolder2
Archive2.r00
Archive2.r01
yyy.txt
subfolder3
Archive3.r00
Archive3.r01
Archive3.r02
Archive3.r04
Archive3.r05
zzz.txt
The files and folders in C:\Users\new-location should look like this:
source folder
subfolder1
xxx.mp4
xxx.txt
subfolder2
yyy.mp4
yyy.txt
subfolder3
zzz.mp4
zzz.txt
A possible batch code for this task is:
#echo off
setlocal EnableDelayedExpansion
set "BaseSourceFolder=C:\Users\Unpack"
set "BaseTargetFolder=C:\Users\New-Location"
for /D %%D in ("%BaseSourceFolder%\*") do (
set "TargetFolder=%BaseTargetFolder%\%%~nxD"
if not exist "!TargetFolder!" md "!TargetFolder!"
"%ProgramFiles%\WinRAR\Rar.exe" x -cfg- -idq -y "%%~fD\*.r??" "!TargetFolder!"
if not errorlevel 1 (
del /F /Q "%%~fD\*.r??"
move /Y "%%~fD\*" "!TargetFolder!">nul 2>nul
rd "%%~fD" 2>nul
)
)
rem rd "%BaseSourceFolder%" 2>nul
endlocal
for /? executed in a command prompt window displays help for command for with parameter /D which means for each directory matched by * in base source folder.
In the loop first the target folder name is defined based on name of the subfolder to process. %%~fD and %%~nxD are also explained by for /? whereby folders usually do not have an extension and therefore %%~nD is often also enough.
Next this target folder is created if not already existing.
Then Rar.exe is executed to extract the multi-volume archive in the current subfolder directly to the defined target folder.
*.r?? is used to make this batch file work for multi-volume archives with old naming scheme ArchiveName.r00, ArchiveName.r01, ... as well as better naming scheme ArchiveName.part01.rar, ArchiveName.part02.rar, ... which is used by default by WinRAR version 5.21. RAR automatically skips the archive files processed already during extraction of a multi-volume archive from the list matching *.r??.
Exit code of Rar.exe is evaluated to determine if any error occurred. If exit code assigned to errorlevel is lower than 1, there was no error and the 3 commands of the if branch are executed resulting in deleting first all RAR archive files.
The remaining files in current subfolder are also moved to the current target folder which is the *.txt file in the folder structure example.
As the current subfolder should be empty now, the command rd should be able to remove the directory. In case of an error because subfolder is still not empty, the subfolder remains in base source folder.
The base source folder is empty if everything worked without an error. The commented line after for loop could be used to remove the empty base source folder as well, but keep the folder if anything failed.
I am new to scripting, can some please assist me,
I have batch file that
Looks at the first 8 characters in the file name, creates and
moves those files to new folder with first 8 characters as folder
name.
Then looks at folder created in step 1 for next four series
of character (9,10,11,12)and create and move to another subfolder
with next 4 characters as folder name.
Then looks at folder created in step 2, for extension of every file and create and move
to a new folder with extension as folder name.
For example, I have files that look like this
ABCEFGHI0703xyz.pdf
STUVWXYZ0805xyz.pptx
Move to folder
ABCEFGHI\0703\PDF
STUVWXYZ\0805\PPTX
Keeping in mind first 8 characters are random, next 4 character are year and month, and 9 types of extensions.
I am using this batch script to create these folders:-
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=C:\sourcedir"
SET "destdir=C:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*" '
) DO (
SET name=%%~na
SET ext=%%~xa
SET name=!name:~0,8!\!name:~8,4!\!ext:~1!
MD "!name!" 2>nul
MOVE "%sourcedir%\%%a" "!name!\" >nul
)
GOTO :EOF
Now I would like to add a WINRAR command to archive just the extension folders created in step 3, I am using this command to create the archives.
C:\ ABCEFGHI\0703\PDF>WINRAR A PDF C:\ ABCEFGHI\0703\PDF
Is it possible to add this command to the script?
Ok first you need to have rar.exe in a folder in %PATH%,
i'd suggest you put a link in your Windows\System32 folder like so:
mklink C:\Windows\System32\rar.exe "C:\Program Files\WinRAR\rar.exe"
then you can get to work.
As you already suggested, first create the desired directory tree and then just add the required files to your archive like so:
rar.exe a %ARCHIVE_NAME% MainFolder\*.pdf
rar.exe a %ARCHIVE_NAME% MainFolder\FolderA\*
rar.exe a %ARCHIVE_NAME% MainFolder\FolderB\*
Whereas %ARCHIVE_NAME% is the file name of your new target archive (such as foo.rar)
This will every *.pdf file in 'MainFolder' and everything in 'FolderA' and 'FolderB'. The directory tree will be preserved.
Also, you may want to check whether %ARCHIVE_NAME% already exists, since rar will just add the specified files to an existing archive (possibly overriding them)
Hope this clarifies some things for you.
Edit: doing this recursivly for unknown root directory
set ARCHIVE_NAME=%CD%\pdf_archive.rar
for /r %CD% %%d in ('PDF') do (
if exist "%%d" (
echo Archiving files in: %%d
rar a "%ARCHIVE_NAME% "%%d"\*
)
)
Now this will go into every subdirectory recursivly (starting from your current directory)
Then iw will look for folders called 'PDF' and if they exist it will archive every file in that folder to %ARCHIVE_NAME%
With a batch (.bat), I want to copy all mp3 files that are in 1 subdirectory of D:\TEMP
D:\TEMP\\(anyfolder)\\(anyfile.mp3)
to
E:\MYFOLDER\
I tried with xcopy but
I don't know how to tell "just recurse subfolders of D:\TEMP and not subsubfolders, subsubsubfolders, etc."
When using xcopy, folders are created in the destination (in order to replicate source's folder tree), I don't want this : files should be copied in just 1 single folder.
for command is your friend. Read help for and then try this in the command prompt
for /d %a in (*) do #echo %a
as you see, it follows all subfolders in the current directory.
thus,
for /d %a in (*) do #copy %a\*.mp3 e:\myfolder
will copy all your mp3 to the destination folder.