Beware, long build up and exposition to question and problem. If you want to see the full question first, see the header below.
I have a batch file that I'm writing that is for automatically moving specific files around. Basically, I have a set up where I have two networks, one connected to the internet and one isolated on an internal network. In order to move files between these two networks, users put files in their own folder on a server so that someone can pull those files onto an external drive and physically move them to a machine on the other network and put those files on a similar folder no the other network.
Here's a crappy picture of the folder layout and the process of moving
Here's the batch file (relevant parts) that move the files. This script not only moves the files between folders, but it also generates a log of all the files that were waiting to be moved.
@echo off
rem Copies the contents of the external drive to the "AlreadyMoved" folders
dir /b /s /a:d "\\SERVER\Path\Users\*AlreadyMoved" | for /f "delims=\; tokens=3,4,5*" %%a in ('findstr AlreadyMoved') do @xcopy /i /s /y "E:\%%b" "\\SERVER\Path\%%a\%%b\%%c\"
set folder="E:\"
cd /d %folder%
IF EXIST "E:\" (
goto :DRIVECLEAN
) ELSE (
echo ERROR: "%folder%" DRIVE NOT CONNECTED!
echo(
echo(
echo PLEASE CONNECT %folder% DRIVE AND TRY AGAIN.
exit /b)
:DRIVECLEAN
set folder="E:\"
rem echo This will wipe the %folder% drive clean!
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
rem gets the current date and time in 24 hr format for the text file name.
set hh=%time:~-11,2%
set /a hh=%hh%+100
set hh=%hh:~1%
set dateseed=%date:~10,4%_%date:~4,2%_%date:~7,2%_%hh%%time:~3,2%
set output="%userprofile%\Desktop\Folder\%dateseed%.txt"
rem creates contents of the log file
echo File Header >> %output%
echo( >> %output%
echo Scanned By: %username% >> %output%
echo Date Scanned: %mydate% >> %output%
echo Time Scanned: %mytime% >> %output%
echo( >> %output%
rem echoes the File Name, Size, and Last Modified date in the log file
for /f "delims=" %%F in (
'dir /b /s \\SERVER\Path\Users ^| findstr ToBeMoved\'
) do for /f "delims=\; tokens=3,4,5*" %%a in ("%%F") do echo %%b: %%d -- Size: %%~zF bytes -- Last Modified: %%~tF >> %output%
rem copies the contents of the "ToBeMoved" folders to the External Drive
dir /b /s /a:d "\\SERVER\Path\Users\*ToBeMoved" | for /f "delims=\; tokens=3,4,5*" %%a in ('findstr ToBeMoved') do @xcopy /i /s /y "\\SERVER\Path\%%a\%%b\%%c" "E:\%%b\"
pause
All this results in a log file that looks something like this:
File Header
Scanned By: username
Date Scanned: 12-08-2016
Time Scanned: 01:03 PM
Last, First1: FileName1.ext -- 10000 bytes -- Last Modified: 12/08/16 2:50 PM
Last, First1: FileName2.ext -- 10000 bytes -- Last Modified: 12/08/16 2:55 PM
Last, First2: FileName3.ext -- 10000 bytes -- Last Modified: 12/08/16 2:50 PM
Last, First2: FileName4.ext -- 10000 bytes -- Last Modified: 12/08/16 2:55 PM
QUESTION STARTS HERE!
So after that extremely long winded build up, here's the question.
I want to get the text file to look like this:
File Header
Moved By: username
Date Moved: 12-08-2016
Time Moved: 01:03 PM
Last, First1
FileName1.ext -- 9 KB -- Last Modified: 12/08/16 2:50 PM
FileName2.ext -- 9 KB -- Last Modified: 12/08/16 2:55 PM
Total Moved: 2 Files
Last, First2
FileName3.ext -- 9 KB -- Last Modified: 12/08/16 2:50 PM
FileName4.ext -- 9 KB -- Last Modified: 12/08/16 2:55 PM
FileName4.ext -- 9 KB -- Last Modified: 12/08/16 2:56 PM
Total Moved: 3 Files
You'll notice there are a few differences with this one and the previous text file but the key difference that I'm trying to figure out is how to count the number of files in a particular user's "ToBeMoved" folder.
I've noticed there's no obvious counting function in batch but I've come across a few methods for counting files in a directory but I can't seem to figure out how to implement those into this script and it actually not blow up.
The obvious solution in my mind would be to implement the count inside the same loop that outputs the contents of the log file. Otherwise, I don't know how I would keep all the user's information together. That said, the loop I have has already taken me forever to cobble together (I'm no expert in batch) and I haven't the faintest clue how to add a complex addition to these loops.
Because see there's an added bonus that (this is not entirely relevant to the scope of this question but I wouldn't object if anyone offered assistance to these issues as well. I'm still researching them and if I can't figure it out, I'll probably have to make separate questions for them.) I need to be able to do some conversions of the size value and add line breaks.
Can anyone think of how I could achieve this? Or at least point me in the right direction?
EDIT: This is in response to elzooilogico's answer. I implemented the changes he suggested and the code executed without any errors but the text file isn't formatted quite right. I put some test files into some folders to see what should happen.
I should have gotten something like this:
File Header
Moved By: username
Date Moved: 12-09-2016
Time Moved: 10:30 AM
Last,First1
FileName1.ext -- Size: 1 KB -- Last Modified: 12/09/2016 10:25 AM
FileName2.ext -- Size: 1 KB -- Last Modified: 12/09/2016 09:28 AM
FileName3.ext -- Size: 1 KB -- Last Modified: 12/09/2016 09:29 AM
FileName4.ext -- Size: 1 KB -- Last Modified: 12/09/2016 09:31 AM
Total Moved: 4 Files
Last,First2
FileName5.ext -- Size: 1 KB -- Last Modified: 12/09/2016 09:31 AM
FileName6.ext -- Size: 1 KB -- Last Modified: 12/09/2016 09:37 AM
FileName7.ext -- Size: 1 KB -- Last Modified: 12/09/2016 09:42 AM
Total Moved: 3 Files
Instead it looked like this:
File Header
Moved By: username
Date Moved: 12-09-2016
Time Moved: 10:30 AM
Last,First1
Last,First1
Last,First1
Last,First1
Last,First2
Last,First2
Last,First2
FileName1.ext -- Size: 103 bytes -- Last Modified: 12/09/2016 10:25 AM
FileName2.ext -- Size: 463 bytes -- Last Modified: 12/09/2016 09:28 AM
FileName3.ext -- Size: 463 bytes -- Last Modified: 12/09/2016 09:29 AM
FileName4.ext -- Size: 531 bytes -- Last Modified: 12/09/2016 09:31 AM
FileName5.ext -- Size: 531 bytes -- Last Modified: 12/09/2016 09:31 AM
FileName6.ext -- Size: 527 bytes -- Last Modified: 12/09/2016 09:37 AM
FileName7.ext -- Size: 527 bytes -- Last Modified: 12/09/2016 09:42 AM
Now it is possible that I somehow implemented it incorrectly because the loop he wrote is definitely above my skill level. But I copied it directly into my code and corrected the generic bits where necessary.
FINAL EDIT: I must have put it in wrong the first time because now it works perfectly!