join files inside every folder using batch command - batch-file

I have 10-20 directories with a number of files inside in each folder.
I want to join in a single file every pack of files inside each folder so if I have 20 folders with 2300 files I want 20 joined files.
Example
INPUT
folder1 - 500 files
folder2 - 340 files
folder3 - 5 files
OUTPUT REQUEST
folder1.txt (500 joined files)
folder2.txt (340 joined files)
folder3.txt (5 joined files)
But I have many folders so I try to find a .bat command to create automatically this operations.
VIDEO: what i want

Assuming each of your files is text file with a newline terminator at the end of each final line, then you can use the following:
for %%F in ("folder1" "folder2" "folder3") do >"%%F.txt" type "%%~F\*"
The name of each file will be output to the screen via stderr.
If you want to hide the file names:
for %%F in ("folder1" "folder2" "folder3") do >"%%F.txt" 2>nul type "%%~F\*"
If you want the file names to be included in the text file output:
for %%F in ("folder1" "folder2" "folder3") do >"%%F.txt" 2>&1 type "%%~F\*"
IF you want to process all folders within your current directory, then change the FOR command for any of the commands listed above to look like:
for /D %%F in (*) do ...

Related

Copying and renaming multiple files in a .bat file with FOR ... DO xcopy

I want to move several thousand files (.jpg and .pdf) from one location to another and then rename each file.
My 'copy_rename_docs.csv' input file contains two columns:
the first column containing the original path AND file names for each file that I want to move and rename
and the second column containing the new paths AND file names that I want to move and rename each file to
like this:
\\myapp\data\e3\9b\e39bf5e8d745833418d3edcd97c6c7589716970b,\\myapp\test_migration\e3\9b\Test File-xxx - SS1.pdf
\\myapp\data\09\f5\09f58d1a1345f67a36154e99ffedb27308fa4292,\\myapp\test_migration\09\f5\nxf7.pdf
so "e39bf5e8d745833418d3edcd97c6c7589716970b" is a SHA1 hash of "Test File-xxx - SS1.pdf"
To copy and rename the files I tried this batch file:
#echo off
FOR /F "tokens=1,2 delims=," %%a IN (copy_rename_docs.csv) DO (xcopy /e /i "%%a" "%%b\*")
pause
exit
But instead of copying and renaming the files to this:
\\myapp\test_migration\e3\9b\Test File-xxx - SS1.pdf
\\myapp\test_migration\09\f5\nxf7.pdf
It is creating a FOLDER for each file with the FILE names as the FOLDER names and copying the files with their original file names into each FOLDER, like this:
\\myapp\test_migration\e3\9b\Test File-xxx - SS1.pdf\e39bf5e8d745833418d3edcd97c6c7589716970b
\\myapp\test_migration\09\f5\nxf7.pdf\09f58d1a1345f67a36154e99ffedb27308fa4292
What I want is:
\\myapp\test_migration\e3\9b\Test File-xxx - SS1.pdf
\\myapp\test_migration\09\f5\nxf7.pdf
What's wrong with my batch file?
And is xcopy the best tool for this?

Copy subfolders with exclusions

I have a messy situation on production server, there is a location on production which should only contain 3 folder however due to not following some process we have got more than 1000 folders and files and I am aiming to clean it via a batch file so that there is no chances of human error.
So I would like to copy all folders and files except 3 folders to a new location. can someone help in this as not able to put logic to exclude these 3 folders.
Create a file called ex.txt that includes 3 lines, each of which is the name of the folder that you would like to exclude from the copy, e.g.:
\folder1\
\folder2\
\folder3\
Now, go to the parent of the high-level directory (say, directory_to_copy), that you would like to copy, in which location exists the ex.txt file, and type
xcopy /e /i /exclude:ex.txt directory_to_copy destination_name
This will exclude the folders folder1, folder2, and folder3 from the copy.
Note: the backslashes \ are important to ensure that the other folders containing those strings (folder1, folder2, and folder3) are not excluded.
This writes tempmove.bat.txt in the same folder as the batch file that contains the move commands to move every folder except the three folders shown (testenv stageenv prodenv).
You can examine the text file before renaming it to .bat to actually use, if it shows the right commands.
Make sure "d:\wrong folders" folder already exists.
#echo off
cd /d "c:\production folder"
(
for /d %%a in (*) do (
if /i not "%%~nxa"=="testenv" if /i not "%%~nxa"=="stageenv" if /i not "%%~nxa"=="prodenv" echo move "%%~fa" "d:\wrong folders\%%~nxa"
)
)>"%~dp0\tempmove.bat.txt"
pause

How to extract all multi-volume RAR archives from subfolders of a folder?

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.

Batch file to upload each numbered file to a different FTP folder

I need to write a batch file that uploads two files via FTP to different folders.
Example: filename_0001.txt copy to folder1 on server1 filename_0002 copy to folder2 on server1.
The names of target files are fixed.
My current batch file uploads only the first file that has the lower number -the only difference in the filenames are the numbers, that are changed daily.
user name >>script.txt
%1 >>script.txt (password as parameter to batch file)
put filename_????.txt folder1
ftp -s:script.txt [server name]
How can the other file with higher number be uploaded? I thought of checking the file names and then put them in the script. Can anyone tell any command to do that?
I need something like this:
put filename_????+1.txt folder2
Prepare a text file (folders.txt) with a sorted list of folders to use, like:
folderA
folderB
folderC
folderD
From a batch file save a sorted list of files to upload to another file (files.txt). Merge the two files together as described in question Combining multiple text files into one.
The code would be like:
#echo off
dir /ON /B filename_????.txt > files.txt
setlocal EnableDelayedExpansion
3< folders.txt (for /F "delims=" %%a in (files.txt) do (
set /P FOLDER=<&3
echo put %%a !FOLDER!
))
The output will be like:
put filename_4100.txt folderA
put filename_4101.txt folderB
put filename_4102.txt folderC
put filename_4103.txt folderD
It won't work when number of digits in filenames is different, as filename_10000.txt will be sorted before filename_9999.txt. For possible improvement, see Naturally Sort Files in Batch

Compare filenames with foldernames and move matching filenames elsewhere

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"

Resources