Batch file / Powershell script to zip files in folders - powershell

I have a lot of folders with files inside for example:
Folder1 with sample content:
File1.txt
File2.txt
File3.xml
File4.jpg
.
.
.
Result after zip: Folder1.zip
Folder2 with sample content:
File12.txt
File22.txt
File32.xml
File42.jpg
.
.
Result after zip: Folder2.zip
How to zip only all files (not entire folder) in every folder with result filename like "Foldername.zip". I found this code for /d %%a in (*) do (ECHO zip -r -p "%%~na.zip" ".\%%a\*") but it zips entire folders, not only files.

I solved the problem with the following steps:
create empty zip file
copy this file to all folders in "working folder"
rename all zip files with folder name
add .txt, .jpg, .xml files to zip.
This is a working code:
#echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('dir /A:D /B') do (
xcopy /y "Path_to_empty_zip\Test.zip" ".\%%~na\"
rename "%%~na\Test.zip" "%%~na.zip"
)
for /f "delims=" %%a in ('dir /A:D /B') do (
cd "Path_to_working_folder\%%~na\"
zip -r "%%~na.zip" "*.txt"
zip -r "%%~na.zip" "*.jpg"
zip -r "%%~na.zip" "*.xml"
)
pause
I found some similar problems without solutions because publish my solution as "answer on my question".

Related

Folder creation - from .bat file to powershell / sharepoint

I use a .bat file to create folders listed in two .txt files and also for deleting empty folders. All works fine but now I need the same to work on sharepoint.
Can anyone describe how or if this can be done? I have read that maybe I need to find put about powershell?
The .bat file looks like this:
echo off
%CHCP 1252
mkdir C:\Temp\MStruktur
Copy "P:\folderstructure\Niv1.txt" "C:\Temp\MStruktur"
Copy "P:\folderstructure\Niv2.txt" "C:\Temp\MStruktur"
cls
REM -----checking for empty characters
for %%a in (.) do set currentfolder=%%~na
if not "%currentfolder%"=="%currentfolder: =%" GOTO error
:home
echo "Folder structure menu:"
echo -------------------------------------
echo - 1 - create level 1
echo - 2 - create level 2
echo.
echo - S - Delete empty folders
echo.
echo - X - Exit
echo.
set /p web=Valg:
if "%web%"=="1" goto niv1
if "%web%"=="2" goto niv2
if "%web%"=="s" goto slet
if "%web%"=="x" goto slut
goto home
:niv1
cls
for /f %%i in (C:\Temp\MStruktur\niv1.txt) do mkdir %~dp0\%%i
echo.
echo Level 1 created
echo.
pause
exit
:niv2
cls
for /f %%i in (C:\Temp\MStruktur\niv2.txt) do mkdir %~dp0\%%i
echo.
echo Level 2 created
echo.
pause
exit
:slet
cls
for /f "delims=" %%d in ('dir %~dp0 /s /b /ad ^| sort /r') do rd "%%d"
echo.
echo Deleted empty folders
echo.
pause
exit
:error
cls
echo.
echo No blanks in folder name
echo.
pause
exit
:slut
RD /S /Q C:\Temp\MStruktur
exit

Delete all files after date, code is slow

I made a question last week about getting a batch file or code to delete all .txt files in a folder that were created before the last 60 days and I was directed to use the code below.
forfiles -p "J:\Test_Files" -s -m *.txt* -d 60 -c "cmd /c del #path"
This code does the job and works fine but it goes too slow deleting 250 files per minute. I need to delete a total of 2,600,000 files and this would take too long.
The code I used below deletes 200 files a second but deletes all .txt files
cd /BASE_PATH
del /s *.txt
How can I edit this code to delete files created before 60 days? I need it to delete at a faster pace.
Thank you for helping! :D
You can try with
#echo off
setlocal enableextensions disabledelayedexpansion
rem Configure script
set "target=J:\Test_Files"
set "fileMask=*.txt"
set "age=60"
rem We will use a temporary file.
for %%t in ("%temp%\%~nx0.%random%%random%%random%.tmp") do (
rem Send to temp file the list of matching files retrieved by the robocopy command
>"%%~ft" robocopy "%target%." "%target%." "%fileMask%" /minage:%age% /l /nocopy /is /s /njh /njs /ndl /nc /ns
rem Process temporary file deleting the selected files
for /f "usebackq tokens=*" %%f in ("%%~ft") do echo del "%%~ff"
rem Once done, remove the temporary file
) & del /q "%%~ft"
del commands are only echoed to console. If the output is correct, remove the echo command.

looping through files for postgresql give error in windows

i'm trying to read files and load them all csv files, and once done move the files to some other location :
cd E:\data\
for /f %%a in (’dir /b filename*.CSV) do (
psql -U postgre -W password -c "COPY INTO LA from %%a" zipcodes
mv %%a E:\data\bc\
)
but it gives me following error:
E:\data>for /F %a in (ΓÇÖdir /b filename*.CSV) do (
psql -U postgre -W password-c "COPY INTO LA from %a" zipcodes
mv %a E:\data\bc\
)
The system cannot find the file ΓÇÖdir.
thanks for help
#echo off
setlocal enableextensions disabledelayedexpansion
set "psql=c:\wherever\psqlIs\psql.exe"
pushd "e:\data" && (
for %%a in ("filename*.CSV") do (
"%psql%" -U postgre -W password -c "COPY INTO LA from %%~a" zipcodes
move "%%~a" "E:\data\bc\"
)
popd
)
for /f is intended for file/string processing. To iterate over a set of files use a simple for
Anyway, the problem in your code are the opening single quote (as Alex K has pointed), the missing closing quote and the non existing mv command in windows that should be move
edited it seems there are problems with file loading. psql copy command indicates it is better to use full path names (with backslashes doubled), so
#echo off
setlocal enableextensions enabledelayedexpansion
set "psql=c:\wherever\psqlIs\psql.exe"
pushd "e:\data" && (
for %%a in ("filename*.CSV") do (
set "file=%%~fa"
"%psql%" -U postgre -W password -c "COPY INTO LA from E'!file:\=\\!'" zipcodes
move "%%~a" "E:\data\bc\"
)
popd
)

How to make a OR statement in a batch file?

Hi want to exclude 2 or more files from my batch file.
My batch file executes all SQL files in that folder.
Here is the code:
ECHO %USERNAME% started the batch process at %TIME% >output.txt
FOR %%G IN (*.sql) DO (
//IF would go here to skip unwanted files
sqlcmd.exe -S RMSDEV7 -E -d ChaseDataMedia7 -i "%%G" >>output.txt
)
pause
So, how would i add and if statemant to skip the files in the loop that i don't want to execute?
You can chain IFs;
FOR %%G IN (*.sql) DO (
if not %%G==foo.sql if not %%G==bar.sql (
some command "%%G"
)
)
#echo off
setlocal EnableDelayedExpansion
set unwantedFiles=foo bar baz
for %%g in (*.sql) do (
set "test=!unwantedFiles:%%~Ng=!"
if "!test!" == "!unwantedFiles!" (
echo %%~Ng.sql is not unwanted, process it:
echo Process with %%g
)
)
Take a name and copy unwantedFiles by removing current name. If the result is the same as before, this name is NOT in unwantedFiles, so process it...

Build a command and execute it from within a batch file

I have a batch file that outputs a list of commands which themselves can be executed at the command line.
Here is a simplified version of what I'm doing:
set %foldername%="c:\my_folder"
set %exename%="c:\my_utility.exe"
cd %foldername%
FOR /F "tokens=*" %%G IN ('dir *.xml /s /b /a:-d') DO #echo %exename% /x="%%G"
This basically outputs a batch file. It looks like this:
c:\my_utility.exe /x="c:\my_folder\file1.xml"
c:\my_utility.exe /x="c:\my_folder\file2.xml"
c:\my_utility.exe /x="c:\my_folder\file3.xml"
c:\my_utility.exe /x="c:\my_folder\file4.xml"
I want to execute these commands. Currently I have to redirect the output to a batch file and then run that. Is there any way to just say "execute this command I just constructed" in the dos prompt?
Just remove echo in the FOR loop:
FOR /F "tokens=*" %%G IN ('dir *.xml /s /b /a:-d') DO %exename% /x="%%G"