Removeing lines in a .eml files and copying the new "files" (destination A) into multiple files (destination B) - windows

Hi I would like to create a batch file which finds certain keywords in .eml files (destination A) and then deletes the line in which they reside. After that I need the batch file to put the "new" files in separate .eml files in (destination B). the files can be .txt as well.
e.g
line 2 needs to be removed which I can do using findstr, however my problem is that after I get the lines removed I can only place the "new file" in one .txt file and I need to place the "new files" in multiple .txt files in the same destination.
`e.g
DESTINATION A "NEW FILE" DESTINATION B
line1: good line1: good File1.txt
line2: error > line2: good > File2.txt
line3: good File.... to however many "new files" i have.`
I have searched for a forcedir type command by I had no luck.
here is the code I use:
`findstr /v /I "2 3 7" C:\A\*.txt >> C:\B\onefileonly.txt
msg * Done!
exit >nul`
<----- this onefileonly.txt is my problem. I need it to be the seperate "new folders".
Inside the onefileonly.txt file
I have also tried this code, however I has the same problem.
`#echo off
echo Removing...
for /f "skip=3 delims=*" %%a in (C:\A\testfile.txt) do (
echo %%a >>C:\B\onefileonly.txt
) >nul
echo Lines removed, rebuilding file...
xcopy C:\B\onefileonly.txt C:\A\testfile.txt /y >nul
echo File rebuilt, removing temporary files
del C:\B\onefileonly.txt /f /q >nul
msg * Done!
exit >nul`

Ok so after about a week i managed to bash together a script which answers this question, I still need to clean it up a bit but here is the code below
::CallScript
#echo off
CALL :ScriptA
CALL :ScriptB
CALL :ScriptC
pause
goto :eof
:ScriptA
del "C:\source\INCOMPLETE MESSAGE*.eml" "C:\source\EXCEPTION ERROR*.eml"
goto :eof
:ScriptB
#echo off
SETLOCAL
FOR %%i IN (C:\source\*.eml) DO (
TYPE "%%i"| more /E +4 >> C:\5500\%%~ni.eml
)
goto :eof
:ScriptC
del "C:\source\ERROR WITH Position Post*.eml"
goto :eof
The script deletes the unwanted email files in source (which are INCOMPLETE MESSAGE & EXCEPTION ERROR )
Then the script takes the email files out of source, removes the top 4 lines in the .eml document copies them to the 5500 folder and lastly deletes the old .eml files in the source folder.

Related

Loop through files in a folder and check if they have different extensions

I have a folder that contains files; each document should have .pdf and .xml format. I need to write a BAT file to run from a scheduled task to verify that both documents exist for each.
My logic is:
loop through files in the folder
strip each file to its name without extension
check that same name files exist for both .xml and pdf.
if not mark a flag variable as problem
when done, if the flag variable is marked, send an Email notification
I know how to use blat to sending email, but I'm having trouble to execute the loop. I found a way to get path and file name without extension but can't merge them.
I've used batch files a few time, before but I'm far from an expert. What am I missing?
Here's the code I have so far:
set "FolderPath=E:\TestBat\Test\"
echo %FolderPath%
for %%f in (%FolderPath%*) do (
set /p val=<%%f
For %%A in ("%%f") do (
Set Folder=%%~dpA
Set Name=%%~nxA
)
echo Folder is: %Folder%
echo Name is: %Name%
if NOT EXIST %FolderPath%%name%.xml
set flag=MISSING
if NOT EXIST %FolderPath%%name%.pdf
set flag=MISSING
)
echo %Flag%
pause
There is no need for fancy code for a task such as this:
#Echo Off
Set "FolderPath=E:\TestBat\Test"
If /I Not "%CD%"=="%FolderPath%" PushD "%FolderPath%" 2>Nul||Exit/B
Set "flag="
For %%A In (*.pdf *.xml) Do (
If /I "%%~xA"==".pdf" (If Not Exist "%%~nA.xml" Set "flag=MISSING")
If /I "%%~xA"==".xml" (If Not Exist "%%~nA.pdf" Set "flag=MISSING")
)
If Defined flag Echo=%flag%
Timeout -1
Something like this :
set "FolderPath=E:\TestBat\Test\"
pushd "%FolderPath%"
for %%a in (*.xml) do (
if exist "%%~na.pdf"(
echo ok
) else (
rem do what you want here
echo Missing
)
)
popd
Is this what you want?
#echo off
setlocal enabledelayedexpansion
set "FolderPath=E:\TestBat\Test\"
echo !FolderPath!
for /f "usebackq delims=" %%f in (`dir !FolderPath! /B`) do (
set /p val=<%%f
For %%A in ("%%f") do (
Set Folder=%%~dpA
Set name=%%~nxA
)
echo Folder is: !Folder!
echo Name is: !name!
if NOT EXIST !FolderPath!!name!.xml set flag=MISSING
if NOT EXIST !FolderPath!!name!.pdf set flag=MISSING
)
echo Flag: !flag!
pause
endlocal
You should reformat your code and keep in mind that the grama for batch file is critical. BTW, if you are trying to update the existing batch variable and read it later, you should enable localdelayedexpansion and use ! instead of %.
Keep it simple:
#echo off
pushd "E:\TestBat\Test" || exit /B 1
for %%F in ("*.pdf") do if not exist "%%~nF.xml" echo %%~nxF
for %%F in ("*.xml") do if not exist "%%~nF.pdf" echo %%~nxF
popd
This returns all files that appear orphaned, that is, where the file with the same name but the other extension (.pdf, .xml) is missing. To implement a variable FLAG to indicate there are missing files, simply append & set "FLAG=missing" to each for line and ensure FLAG is empty initially. Then you can check it later by simply using if defined FLAG.
Note: This does not cover the e-mail notification issue. Since I do not know the BLAT tool you mentioned, I have no clue how you want to transfer the listed files to it (command line arguments, temporary file, or STDIN stream?).
In case there is a huge number of files in the target directory, another approach might be better in terms of performance, provided that the number of file system accesses is reduced drastically (note that the above script accesses the file system within the for loop body by if exist, hence for every iterated file individually). So here is an attempt relying on a temporary file and the findstr command:
#echo off
pushd "E:\TestBat\Test" || exit /B 1
rem // Return all orphaned `.pdf` files:
call :SUB "*.pdf" "*.xml"
rem // Return all orphaned `.xml` files:
call :SUB "*.xml" "*.pdf"
popd
exit /B
:SUB val_pattern_orphaned val_pattern_missing
set "LIST=%TEMP%\%~n0_%RANDOM%.tmp"
> "%LIST%" (
rem // Retrieve list of files with one extension:
for %%F in ("%~2") do (
rem /* Replace the extension by the other one,
rem then write the list to a temporary file;
rem this constitutes a list of expected files: */
echo(%%~nF%~x1
)
)
rem /* Search actual list of files with the other extension
rem for occurrences of the list of expected files and
rem return each item that does not match: */
dir /B /A:-D "%~1" | findstr /L /I /X /V /G:"%LIST%"
rem // Clean up the temporary file:
del "%LIST%"
exit /B
To understand how it works, let us concentrate on the first sub-routine call call :SUB "*.pdf" "*.xml" using an example; let us assume the target directory contains the following files:
AlOnE.xml
ExtrA.pdf
sAmplE.pdf
sAmplE.xml
So in the for loop a list of .xml files is gathered:
AlOnE.xml
sAmplE.xml
This is written to a temporary file but with the extensions .xml replaced by .pdf:
AlOnE.pdf
sAmplE.pdf
The next step is to generate a list of actually existing .pdf files:
ExtrA.pdf
sAmplE.pdf
This is piped into a findstr command line, that searches this list for search strings that are gathered from the temporary file, returning non-matching lines only. In other words, findstr returns only those lines of the input list that do not occur in the temporary file:
ExtrA.pdf
To finally get also orphaned .xml files, the second sub-routine call is needed.
Since this script uses a temporary file containing a file list which is processed once by findstr to find any orphaned files per extension, the overall number of file system access operations is lower. The weakest part however is the for loop (containing string concatenation operations).

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
)

Moving files with spaces in name batch scripting

So I'm trying to mimic some features of Apple
One feature I am currently working on is being able to select a single or multiple files then through the "context menu" creating a "new folder" (directed to a batch file passing along file through FOR loop) then moving the files into that "new folder"
The issue I am having is with file names with spaces I researched using "robocopy" but finding it tricky alittle tricky
My code so far
#echo off
set cDir=%~dp1
set newFolder="%cDir%NewFolder"
md %newFolder%
echo.
:: get each selected file and echo
for %%I in (%*) do (
echo %%I
echo.
echo %newFolder%
move "%%I" "%newFolder%"
echo.
)
pause
Your modified code should move files and folders as well. To be on the safe side, here is my variant of your code. Note quotation changes in set, md and move statements but I repeat: your (modified) variant of quotation should work as well:
#echo off
set cDir=%~dp1
set "newFolder=%cDir%NewFolder"
md "%newFolder%"
echo.
:: get each selected file and echo
for %%I in (%*) do (
echo %%I
echo.
echo %newFolder%
move "%%~I" "%newFolder%\"
echo.
)
pause
Although move /? says Moves files and renames files and directories, both Source and Target may be either a folder or a single file (resource, verified on my Win-8).
Proof.
==>move "D:\Path\COCL\bu bu bu" "D:\Path\content\"
1 dir(s) moved.
==>move "D:\Path\content\bu bu bu" "D:\Path\COCL\"
1 dir(s) moved.
==>
Next resource: Command Line arguments (Parameters).

windows batch command to move all wanted sub-folders recrusively in a directory to a NEW folder

I have a local Folder-A which have many subfolders (svn-repository), these subfolders both have a subfolder named .svn. Now I want to move all .svn folders to Folder-B, but incase of oneday I wants the .svn folders back to their original place so I can SVN update them, so the ".svn" folder's parent folder info must be exits in Folder-B
starts like this:
Folder-A
|_DR1
|_.svn
|_s1
|_.svn
|_s1_s1folder
|_.svn
|_file1
|_file1
|_s2
|_.svn
|_file1
|_file2
|_DR2 ... etc
Desired MOVE result:
Folder-A only have actual sub-folders and data but without .svn folder
Folder-B
|_DR1
|_.svn
|_s1
|_s1_s1folder
|_.svn
|_.svn
|_s2
|_.svn
|_DR2 ... etc
Desired RESTORE result:
Folder-A & Folder-B back to where we starts.
please help write commandlines to accomplish the MOVE and RESTORE mission, thank you
#ECHO OFF
SETLOCAL
:: establish source and destination directorynames; ensure dest exists
SET source=c:\folder-a
SET dest=u:\folder-b
MD %dest% 2>NUL
:: delete a tempfile if it exists
DEL "%temp%\svntmp2.tmp" 2>nul
:: create a dummy directory
MD c:\dummy
:: go to root of tree containing SVN directories
PUSHD "%source%"
XCOPY /L /s . c:\dummy |find /i "\svn\" >"%temp%\svntmp1.tmp"
POPD
:: goto destination directory
PUSHD "%dest%"
FOR /f "delims=" %%i IN ('type "%temp%\svntmp1.tmp"') DO CALL :moveit "%%i"
FOR /f "delims=" %%i IN (
'TYPE "%temp%\svntmp2.tmp"^|sort /r'
) DO RD "%%i" /S /Q
POPD
:: delete tempfiles
del "%temp%\svntmp1.tmp"
del "%temp%\svntmp2.tmp"
:: delete the dummy directory
RD /s /q c:\dummy
GOTO :eof
:moveit
:: get filename, remove quotes, then remove leading '.'
SET sourcefile=%~1
SET sourcefile=%sourcefile:~1%
FOR %%i IN ("%sourcefile%") DO (
MD ".%%~pi" 2>nul
MOVE "%source%%%~i" ".%%~pnxi"
>>"%temp%\svntmp2.tmp" ECHO %source%%%~pi
)
GOTO :eof
Set your source and destination directories.
Make a dummy empty directory
go to your source and generate a list of the files to be moved using
XCOPY/L and filtering for lines containing \svn\
go to your destination directory
move each file
remove the leading '.' from the XCOPY output
create the destination subdirectory; ignore 'it already exists' errors
move the file
record the directory name in a second tempfile
Sort the second tempfile in reverse, which puts the deepest subdirectories first
remove the directories, ignoring 'it is not there' errors
delete the tempfiles and dummy directory.
Note that the commands to actually DO the moves/directory creation/deletion are merely ECHOed. This is so that you can verify that the routine does what you want it to do. You would need to verify that it's correct, then remove the ECHO keyword from the statements to actually ACTIVATE the move. Test against a small dummy area first for safety's sake.
As for restoring - it's relatively easy.
xcopy /s/e/v "...folder-b\dr1\svn\" "\folder-a\dr1\svn\"
should do what you want, keeping the restored projects files in folder-b (delete if you like - it's your system) and you can restore on a project-by-project basis.
(I've used SVN throughout because the dot is getting a little small for me nowadays...)

batch file to check if the specific word "test" exist in the files, IF exist copy the whole folder to another location

How can I check if the word "test" exist in any files inside a folder(that contain tons of folders and files) .. and if exist, copy the whole folder to another location.
Please help thank you
findstr /c:"test" *.txt > NUL
if not errorlevel 1 xcopy *.* anotherlocation
If you want to check all files beneath current folder at any level, add /S switch in findstr command. Do the same in xcopy command to copy the whole folder structure.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
findstr /c:"Finished schedule" E:\Init.log > NUL
if "!errorlevel!"=="0"
( echo "OK" )
else ( echo "NOK" )

Resources