How to create folders and files from text file with same names to insert with corresponding name? - windows

I was inspired on works of our colleagues from portal
Have a batch create text files based on a list
Batch script to read input text file and make text file for each line of input text file
to develop creation from a text file for example
"comps.txt" ( comp1,comp2....
for each PC a batch script read from a list of PCs in a text file and create a text file for each line of text as local.
Later in code are created folders files named - the same names ( comp1,comp2....
at the end we have text files : comp1.txt ,comp2.txt ... and folders: comp1 , comp2.... till 400 computers.
Any idea how to add in code or write another separate batch code to move for each coresponding folder text file for text file
comp1.txt->comp1 folder
comp2.txt->comp2.txt
....
We have more than 400 lines!!
I am very begginers for any scrips working very hard every single day and this is my first question. My code in Windows Batch is below
#echo off
setlocal
for /f "tokens=*" %%a in (comps.txt) do (type nul>"%%a.txt")
for /f "tokens=*" %%a in (comps.txt) do (
echo This is line 1 of text>"%%a.txt"
)
for /f %%i in (comps.txt) do mkdir %%i
do (
echo "%%i.txt"
)
endlocal
[]

It doesn't make any sense to generate text files and then folders to move the files to. Create the folders first place and create the files into the folders. Using a (code block) only one for loop is needed.
#echo off
setlocal
for /f "tokens=*" %%a in (comps.txt) do (
if not exist "%%a" mkdir "%%a"
echo This is line 1 of text>"%%a\%%a.txt"
)
endlocal

Related

Batch file for file merge

I have one folder inside that there 15 sub folder Folder1, Folder2, Folder3, etc... Each subfolder (Folder1, Folder2) has 2 or 3 level subfolders. Each folder has some .txt files. I would like to create a batch file that can 15 create merge files for each folder.
I can do it with cmd commands like for %f in (*.txt) do type "%f" >> output.txt.
In the above command, I need to open 15 different command prompt windows and run the above command.
Is there any way that I can achieve this programmatically like in a batch file?
The following batch will create in all direct subfolders of Mainfolder a file Merge.txt containing all text files of this subfolder and below.
It inserts a Header with full path for each text file and appends an empty line to avoid joining lines if the last line of a file doesn't end with a cr/lf.
#Echo off
Set "MainFolder=C:\PathTo\wherever"
For /D %%A in ("%MainFolder%\*") do (
For /F "Delims=" %%B in ('Dir /B/S "%%A\*.txt" 2^>NUL^|Find /V "Merge.txt"') Do (
Echo ==== %%~fB ====
Type "%%~fB"
Echo:
) "
) > "%%~fA\Merge.txt

Read a file name from a text file then search the hard drive for it using batch script

I am just beginning to write my own batch scripts and currently I am trying to develop a small batch script to read a text file containing a list of specific file names with extensions one at a time. After reading the file name, search the hard drive for that file and if found, move it to a specific directory.
This is the code I have developed thus far. It works as long as the files you are looking for are in the same directory. The OS I am using is Windows 8.1.
#echo off
cls
echo.
echo.
echo.
for /F "delims=" %%a in (VIRUS_LIST.txt) do (
echo searching for %%a
echo.
For /R %%G in (%%a) do (
if exist %%a (
copy "%%G" "c:\vault"
echo moving file at %%G to c:\vault
echo.
)
)
)
pause
Here is how I would like it to work; 1. read the file name from the VIRUS_LIST.TXT file, 2. scan drive C for that file, 3. If found copy or move it to a directory on C drive called C:\vault.
The list file I am using is called VIRUS_LIST.TXT that holds the file names to search for, and if found they would be moved to C:\VAULT to be processed later. If anyone can offer any help it would be greatly welcomed.

Apply a batch script to all the csv file of my directory

I have a script that allow me to extract a specific columns from a csv file.
My aim subject is to apply my script to all csv files of my directory.
#echo off>fourcol.csv
setlocal
for /f "tokens=1-22* delims=," %%1 in (ManyColumns.csv) do (
echo %%4,%%5,%%6>>fourcol.csv
)
type fourcol.csv
Apply the script to all the csv file in my directory
Remove duplicates
FileA.csv ...FileZ.csv
server1,Dell,1.0.1,server1,Dell,28/06/2016,...
server2,Hp,1.0.2,server2,Hp,29/06/2016,...
server3,Dell,1.2.1,server3,Dell,30/06/2016,...
server4,Hp,1.3.1,server4,Hp,27/06/2016,...
server3,Dell,1.2.1,server3,Dell,30/06/2016,...
server4,Hp,1.3.1,server4,Hp,27/06/2016,...
My CSV files have the same header and the same data.
Output after applying the script (without duplicates):
server1,Dell,1.0.1
server2,Hp,1.0.2
server3,Dell,1.2.1
server4,Hp,1.3.1
How do I apply the script to all the csv files in my directory?
A couple of small changes should do it:
Don't use numbers as for loop variables. They are reserved for command line parameters.
Use dir /b *.csv to get the list of files to process.
Start from token 1 not token 4 to extract the first three columns.
Use the first for loop to process each file, and a second (nested) for loop to process the lines within the file.
Note:
delims=, may not do what you want (if there are values in the file that contain commas ,).
Try the following batch file (test.cmd):
#echo off
setlocal
rem remove output file if it already exists
if exist fourcol.csv del fourcol.csv
rem loop through the csv files
for /f "usebackq" %%a in (`dir /b *.csv`) do (
rem loop through the lines in each file
for /f "usebackq tokens=1,2,3 delims=," %%b in (%%a) do (
echo %%b,%%c,%%d>>fourcol.csv
)
)
rem add code here to strip duplicates
endlocal
test.csv:
server1,Dell,1.0.1,server1,Dell,28/06/2016,...
server2,Hp,1.0.2,server2,Hp,29/06/2016,...
server3,Dell,1.2.1,server3,Dell,30/06/2016,...
server4,Hp,1.3.1,server4,Hp,27/06/2016,...
server3,Dell,1.2.1,server3,Dell,30/06/2016,...
server4,Hp,1.3.1,server4,Hp,27/06/2016,...
fourcol.csv:
server1,Dell,1.0.1
server2,Hp,1.0.2
server3,Dell,1.2.1
server4,Hp,1.3.1
server3,Dell,1.2.1
server4,Hp,1.3.1
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
dir - Display a list of files and subfolders.
for /f - Loop command against the results of another command.

Batch file to sort files and list missing

I am trying to write a batch file that would read rows/lines from a txt file containing a list. The batch file would then copy the documents that match, and produce a list of missing files.
So far, the code successfully copies the files that it matches, but it also fills the "Missing.txt" file will exact contents of the input list, rather than simply the missing files.
#echo off
::Requests name of list file to be used by batch file
echo Enter list file name and press enter
set /p var=
mkdir %userprofile%\Desktop\%var%\
set /A lis=1
::Logic to search for files based on contents of list inputted by user at start.
for /f "tokens=*" %%i in (%var%.txt) DO (
call :processline %%i
IF NOT EXIST %%i (echo %%i>>%userprofile%\Desktop\%var%\Missing.txt)
)
pause
::Function called processline
::Assigns a string/value to variable "line"
::Copies a file with name = "line" to the user's desktop
::Renames the file to include a number reference, based on original list being searched
::Increments number for next file to be searched,copies and renamed
:processline
echo line=%*
xcopy /s %*.* %userprofile%\Desktop\%var%
move /Y %userprofile%\Desktop\%var%\%*.pdf %userprofile%\Desktop\%var%\%lis%_%*.pdf
set /A lis=%lis%+1
:eof
I suspect my problem is within the "for" logic, although there might be a way to input missing file names within the processline function.
Any help or advice would be much appreciated.
It's command order: :processline subroutine moves something, maybe including %%i file. I'd use next code snippet:
IF EXIST "%%~i" (
call :processline %%i
) ELSE (
>>"%userprofile%\Desktop\%var%\Missing.txt" echo %%i
)

copy file based on content - command prompt

I got a folder with bunch of files in it. each file name has unique date ex: 20140101. each file has data of one product.. ex: 20140101 has data for product "oranges" file 20140102 has data of product "apple" .. I need to create a script which will check the content of file and if match found, copy the file to different directory.
Example:
Find "Oranges" in
C:\Data\
found 2 files .. 20131229 and 20140101
copy files to c:\oranges\
Thanks in advance.
#echo off
setlocal enableextensions disabledelayedexpansion
set "source=c:\data\*.*"
set "target=c:\"
set "classes=orange apple"
for %%c in (%classes%) do (
if not exist "%target%%%c\" md "%target%%%c\"
for /f "delims=" %%f in ('findstr /m /l /c:"%%c" "%source%"') do (
copy "%%~ff" "%target%%%c\"
)
)
For each of the indicated classes, create target directory if it does not exist, and for each file with the indicated content, move it to the adecuated directory (not tested, adapt as needed)

Resources