Create folders and files from txt batch script - batch-file

I've already found most of the answers, but I still can't use the batch file to create what I need.
I need to create folders according to the list in folders.txt.
Create an item.cs.md file in the folders.
Write the content from the content.txt file to the file one by one.
Folders are already creating me files as well, but the content is always filled with just the one from the underline content.txt line.
#echo off
SETLOCAL
for /f "delims=" %%l in (content.txt) do (
for /f "tokens=1" %%a in (folders.txt) do (
if not exist "%%a" mkdir "%%a"
ECHO %%l>%%a\item.cs.md
))
Thanks for your time and tips.
Documentation:
How to create multiple text files each with different names using a batch file?
create folders from each line of a textfile in bat
How to create folders and files from text file with same names to insert with corresponding name?
Batch File To Copy Every Line In .txt File And Then Create New .txt File For Each Line

Related

How to insert text into multiple text files using a .bat file?

I having drouble to create a batch file that can insert text into multiple text files at once.
What I can do just now is that one file that I drop on the batch file is getting inserted with the text "I AM PROGRAMMING".
But I would like to drag multiple text files and that all of it/each every text file gets inserted with that text.
#echo off
echo I AM PROGRAMMING> "%~1"
Or if there is somehow possible to do so every text file in a specific place/folder gets inserted with a specific text (for example "I AM PROGRAMMING")?
Your solution processes each matching file in the folder, but you said I would like to drag multiple text files.
It's easy to process "multiple dragged files": %* is "all parameters". Just use a plain for loop to process each of them:
#echo off
for %%a in (%*) do echo I AM PROGRAMMING> "%%~a"
(note that this overwrites the files with the one new line; if you want to append, use >> instead of >)
In case you want to process "all .txt files in the folder of the one file I dragged":
for %%a in ("%~dp1\*.txt") do ...
Or "all files in the same folder of the one file I dragged and the same extension as the dragged file":
for %%a in ("%~dp1*%~x1") do ...
I found my solution!
#echo off
cd "yourfilepath"
for %%a in (*.txt) do (echo I AM PROGRAMMING> %%a)
I merged these two helpful information.
How can I use a batch file to write to a text file?
And
Append multiple files using a .bat

Batch file to run exe with unknown filename as arguments

I have an exe for comparing 2 .csv files. And it works when I open cmd window, drag exe, type space, drag 1st csv file, type space, and drag 2nd file.
Now I want to automate it, and the problem is it is never on the same place, nor uses the same files for comparing...
What I got so far is following in batch:
%~dp0\Komparator.exe BC101.csv BC102.csv
pause
those 2 csv files are going to be in same folder, next to .exe and .bat file... but that "same folder" is not always the same, today its one location, another day is another folder
but I dont know how to automate those 2 arguments for file names, I just want it to recognize two .csv files near .exe and .bat
Thanks in advance!
You can read csv filenames and store them into batch variables using for cycle.
Here's an example, assuming your csv files are in the same directory as the batch file.
compare.cmd
#echo off
set FILE1=
set FILE2=
for /f "delims=" %%a in ('dir /b %~dp0*.csv 2^>nul ^| sort') do (
if defined FILE1 set "FILE2=%%~a" & goto :exit_loop
set "FILE1=%%~a"
)
:exit_loop
if not defined FILE1 echo first csv file not found!& goto :eof
if not defined FILE2 echo second csv file not found!& goto :eof
%~dp0\Komparator.exe "%~dp0%FILE1%" "%~dp0%FILE2%"

How to rename the files in the path with the new names from the .txt file in batch?

I have two .txt files. The first one contains the list of pathes to the CD-Images:
C:\Users\N\Desktop\LOG_Dateien_CD_Imaging\BFU_KONGRESS_9.ISO
C:\Users\N\Desktop\LOG_Dateien_CD_Imaging\NDC2005.ISO
The second one contains the new names for this files
490628001
684654326
So the file BFU_KONGRESS_9.ISO in the directory (not in the .txt file!) should be renamed to 490628001.ISO
and NDC2005.ISO to 684654326.ISO. The renaming should go line per line
you need a way to read two files in parallel:
#echo off
setlocal enabledelayedexpansion
<out.txt (
for /f "delims=" %%a in (in.txt) do (
set /p out=
echo rename "%%~a" "!out!"
)
)
Another way: read both files (one after the other) into two arrays and then work with the array variables, but it's more code and might have issues with very large files.

Check file name before deletion (windows Batch)

Is there a way using Windows Batch to read a txt file list of file names. And if the name is present in the list to preserve that file but. If the file name is not found in the txt list that it is deleted.
I namely trying to filter out a horde of temporary files automatically generated by not cleaned out by AutoCAD.
The reason for reading a txt list would allow me to add or delete file names for processing.
Yes this is possible with Windows batch. Assuming you got a list of files FilesToKeep.txt with filenames like this:
WillBeKept.txt
WillAlsoBeKept.doc
in the same directory as the following batch script:
#echo OFF
pushd "%~dp0"
set "PathToCleanup=E:\Temp\Test"
for /F "tokens=*" %%F in ('dir /S/B/A-D %PathToCleanup%\*.*') do (
findstr /S %%~nxF FilesToKeep.txt >NUL || del /Q "%%F" )
This script loops through all files in PathToCleanup and if a filename is not present in FilesToCleanup.txt then the file will be deleted.

.bat file to loop through folder and append text files

I would like to set up a simple batch file that would loop through all the .txt files in a folder (the folder where the batch file is placed), and add the same heading line to each of those files. The heading line is defined in a separate text file.
So for example, let's say I have:
c:\SomeFolder\Headings.txt
--> I want to add this to the top of each of the text files in:
c:\SomeFolder\FolderWithTextFiles\
--> ...by running the batch file:
c:\SomeFolder\FolderWithTextFiles\BatchFile.batch
Extra notes:
- No need to loop through subfolders
Windows batch does not have a native command to edit a file in place (other than to append data to it). So for each file, you need to create a temporary file with the desired content and then delete the original and rename the temp to the original. The delete and rename can be accomplished with a single MOVE command.
#echo off
set "header=c:\SomeFolder\Headings.txt"
set "folder=c:\SomeFolder\FolderWithTextFiles"
set "tempFile=%folder%\temp.txt"
for %%F in ("%folder%\*.txt") do (
type "%header%" >"%tempFile%"
type "%%F" >>"%tempFile%"
move /y "%tempFile%" "%%F" >nul
)

Resources